简体   繁体   English

Spring Boot Controller未注册,获取404-包结构正常

[英]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). 我对此感到非常沮丧... Spring Boot没有注册控制器(至少我在日志中没有看到它)。

The controller class is located in the package UNDER package of main app class... Which is the most common issue that people came across. 控制器类位于主应用程序类的UNDER包中...这是人们遇到的最常见的问题。

In my case, I'm using Maven and following dependencies (giving you only the crucial ones): 就我而言,我正在使用Maven和以下依赖项(仅为您提供关键的依赖项):

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. 我还在Hibernate中使用一些依赖项。 In the log, I can see only things related to Hibernate - you can see it here . 在日志中,我只能看到与Hibernate相关的内容-您可以在此处看到它。 When I'm hitting one of the controller's URLs, I'm getting this in Postman: 当我点击一个控制器的URL时,我在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. 但是没有其他事情真正发生……我完全不明白,这是我第一次遇到像控制器这样的琐碎问题……而在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. 更新#1我注意到日志记录依赖关系有些混乱...仅记录了与Hibernate相关的内容,这使我认为还可以,但是我已经进行了一些清理,并且日志现在很明显。 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 您尚未指定应该使用的http方法

@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. 在调试器的帮助下,我注意到/ hello确实被称为...因为它只是一个测试控制器,所以我开始研究真正的问题,即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). 然后我注意到这个@RestController("mainPath")实际上并没有按我的预期工作(它在每个方法路径之前都提供了额外的路径)。 So the solution was to write 所以解决方案是写

@RestController
@RequestMapping("mainPath")

And so it works as intended :) 所以它按预期工作:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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