关于搭建Spring boot 在启动时RequstMappingHandlerMapping无法找到contorller的解决方案

问题/表象:

Whitelabel Error Page

This application has no explicit mapping for /error, so you are seeing this as a fallback.

Wed May 09 15:32:02 CST 2018

There was an unexpected error (type=Not Found, status=404).

No message available

 

Console控制台:

 

2018-05-09 15:31:20.500  INFO 1612 --- [           main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@60438a68: startup date [Wed May 09 15:31:15 CST 2018]; root of context hierarchy

 

2018-05-09 15:31:20.700  INFO 1612 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)

 

2018-05-09 15:31:20.703  INFO 1612 --- [           main] 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)

 

原因/解决方法:

spring boot 默认扫描项目的类是 在启动类的当前包和下级包,所以如果启动类和controller的类分别放置在不同包下,controller类所属的包必须为启动类所属包的子包。下面给出出错的包路径和正确的包路径:

出错的:

关于搭建Spring boot 在启动时RequstMappingHandlerMapping无法找到contorller的解决方案_第1张图片

正确的:

关于搭建Spring boot 在启动时RequstMappingHandlerMapping无法找到contorller的解决方案_第2张图片

 

INFO 1784 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/hello]}" onto public java.lang.String com.colde.rootapp.controller.HelloSpringBoot.hello()

 

2018-05-09 15:59:52.450  INFO 1784 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)

 

2018-05-09 15:59:52.451  INFO 1784 --- [           main] 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)

关于搭建Spring boot 在启动时RequstMappingHandlerMapping无法找到contorller的解决方案_第3张图片

你可能感兴趣的:(SpringBoot)