简体   繁体   中英

New REST request in JHipster returning “Not Found - 404”

Error Description

Hey all,

I'm having trouble getting a response from my manually added controllers in a JHipster-based project. I scaffolded up the original project, and then hand-wrote my own services and controllers.

When I execute the call, the error result I get from SoapUI (which I am using for initial validation) is at the following url: http://imgur.com/04FpmEZ,Havk1EL#0

And if I look at my Eclipse console error , I see the following: http://imgur.com/04FpmEZ,Havk1EL#1

Controller

/**
 * GET  /courses/json -> get all the courses.
 */
@RequestMapping(value = "/json",
        method = RequestMethod.GET,
        produces = "application/json")
@Timed
public List<Course> getAll() {
    log.debug("REST request to get all Courses");
    return courseService.findAllCourses();
}

Service

package com.testapp.myapp.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.testapp.myapp.domain.Course;
import com.testapp.myapp.repository.CourseRepository;

@Service
@Transactional
public class CourseServiceImpl implements CourseService {

    @Autowired
    CourseRepository courseRepository;

    public long countAllCourses() {
        return courseRepository.count();
    }

    public void deleteCourse(Course course) {
        courseRepository.delete(course);
    }

    public Course findCourse(Integer id) {
        return courseRepository.findOne(id);
    }

    public List<Course> findAllCourses() {
        return courseRepository.findAll();
    }

    public List<Course> findCourseEntries(int firstResult, int maxResults) {
        return courseRepository.findAll(new org.springframework.data.domain.PageRequest(firstResult / maxResults, maxResults)).getContent();
    }

    public void saveCourse(Course course) {
        courseRepository.save(course);
    }

    public Course updateCourse(Course course) {
        return courseRepository.save(course);
    }
}

What is confusing about this is that I ran the query provided by hibernate directly against my DB, and it returns the record set just fine. Is it possible that the service is being blocked due to some security or authentication constraint auto-loaded by JHipster?

A few issues existed, all related to migrating from Roo into JHipster:

  1. I had built my new Controller class with org.sprinframework.stereotype.Controller's @Controller annotation, rather than @RestController ... The original controller annotation was scaffolded up by Spring Roo (which is highly effective at generating services from an existing DB using their DBRE addon, I might add).

  2. After switching over to @RestController, I ran into the second hurdle, which I had originally expected as a JHipster implementation : the service was being blocked due to authentication constraints .

    • This was fixed by going into com.[projectname].config and updating the SecurityConfiguration.java file, exposing specifically the APIs that I wanted.
  3. Then, I had to make sure Hibernate was getting the full collection of the objects being requested (I had a lot of complex relational entities being built by Roo)... failed to lazily initialize a collection of role ...

Voila! Functioning, secure-able JSON-based APIs, fully reverse engineered from an existing Postgresql DB, loaded into a prescaffolded Angular front-end.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM