简体   繁体   中英

Spring Boot Controller not registered, gets 404 - package structure OK

I'm so frustrated with this one... Controller is not being registered by Spring Boot (at least I don't see it in logs).

The controller class is located in the package UNDER package of main app class... Which is the most common issue that people came across.

In my case, I'm using Maven and following dependencies (giving you only the crucial ones):

spring-boot-starter-data-jpa
spring-boot-starter-web
spring-boot-starter-validation
spring-boot-configuration-processor

I'm also using some dependencies for Hibernate. In the log, I can see only things related to Hibernate - you can see it here . When I'm hitting one of the controller's URLs, I'm getting this in Postman:

{
    "timestamp": 1515870369837,
    "status": 404,
    "error": "Not Found",
    "message": "No message available",
    "path": "/hello"
}

Then, additional log comes up:

sty 13, 2018 8:06:01 PM org.apache.catalina.core.ApplicationContext log
INFO: Initializing Spring FrameworkServlet 'dispatcherServlet'

But nothing else really happens... I completely don't get it, it's the first time I've got an issue with sth such trivial like controller... And really couldn't find a solution on this one on StackOverflow.

Here's the code of the controllers I'm using:

@RestController("/parkingPlaces")
public class ParkingPlacesController {

    @Resource
    ParkingPlaceService ppService;

    @RequestMapping("/find")
    List<ParkingPlaceDTO> findParkingPlaces(@RequestBody GpsDTO position) {
        return ppService.findParkingPlaces(position.getLat(), position.getLon());
    }

    @RequestMapping("/find/ray")
    List<ParkingPlaceDTO> findParkingPlacesWithinRadius(@RequestBody GpsDTO position) {
        return ppService.findParkingPlaces(position.getLat(), position.getLon(), position.getRay());
    }

}

The other one is even simpler:

@Controller
public class TestController {

    @RequestMapping("/hello")
    public String hello() {
        return "Hello!";
    }

}

Please help guys :(

UPDATE #1 I've noticed that there was some mess with logging dependencies... Only Hibernate-related things were logged, which made me think it was OK, but I've done some clean-up and logs are evident now. I can see that controllers paths are being registered...

2018-01-14 20:34:57.602  INFO 8572 --- [  restartedMain] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
2018-01-14 20:34:59.253  INFO 8572 --- [  restartedMain] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@3290cd1d: startup date [Sun Jan 14 20:34:46 CET 2018]; root of context hierarchy
2018-01-14 20:34:59.506  INFO 8572 --- [  restartedMain] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/find]}" onto java.util.List<pl.ppclone.dto.ParkingPlaceDTO> pl.ppclone.controller.ParkingPlacesController.findParkingPlaces(pl.ppclone.dto.GpsDTO)
2018-01-14 20:34:59.508  INFO 8572 --- [  restartedMain] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/find/ray]}" onto java.util.List<pl.ppclone.dto.ParkingPlaceDTO> pl.ppclone.controller.ParkingPlacesController.findParkingPlacesWithinRadius(pl.ppclone.dto.GpsDTO)
2018-01-14 20:34:59.513  INFO 8572 --- [  restartedMain] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/hello],methods=[GET]}" onto public java.lang.String pl.ppclone.controller.TestController.hello()
2018-01-14 20:34:59.524  INFO 8572 --- [  restartedMain] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
2018-01-14 20:34:59.527  INFO 8572 --- [  restartedMain] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
2018-01-14 20:34:59.607  INFO 8572 --- [  restartedMain] o.s.w.s.h.BeanNameUrlHandlerMapping      : Mapped URL path [/parkingPlaces] onto handler '/parkingPlaces'
2018-01-14 20:34:59.654  INFO 8572 --- [  restartedMain] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-01-14 20:34:59.654  INFO 8572 --- [  restartedMain] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-01-14 20:34:59.826  INFO 8572 --- [  restartedMain] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-01-14 20:35:00.528  INFO 8572 --- [  restartedMain] o.s.b.d.a.OptionalLiveReloadServer       : LiveReload server is running on port 35729
2018-01-14 20:35:00.730  INFO 8572 --- [  restartedMain] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2018-01-14 20:35:00.868  INFO 8572 --- [  restartedMain] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)

You haven't specified http method you either should go

@RequestMapping(value = "/hello", method = RequestMethod.GET)

or

@GetMapping("/hello")

I managed to found the solution. With the help of debugger I've noticed that /hello was indeed called... As it was only a test controller, I've started to playing with the real problem, which was RestController. Then I've noticed that this @RestController("mainPath") actually doesn't work as I intended (which was providing additional path before each methods path). So the solution was to write

@RestController
@RequestMapping("mainPath")

And so it works as intended :)

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