spring boot入门实例报404问题

Posted tamo

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了spring boot入门实例报404问题相关的知识,希望对你有一定的参考价值。

今天突然想对spring boot写个hello world,应用成功启动后,访问http://localhost:8080/hello报404。

场景说明:controller类所在包:com.yjl.test.controller  ,,,spring boot应用启动类所在包:com.yjl.test.springboot,按网上所查spring boot应用扫描类的说法,于是在应用启动类加有@ComponentScan(basePackages = {"com.yjl.test.controller.*"}) ,,项目成功启动后,仍是报404,,控制台日志中的s.w.s.m.m.a.RequestMappingHandlerMapping 也没有出现“/hello”的Mapped映射。

解决:

spring boot 默认扫描项目的类是 在启动类的当前包和下级包,controller类所属的包必须为启动类所属包的子包。。(虽然有说启动类和controller的类可分别放置在不同包下,再利用ComponentScan注解,但我没测通过.......)

 将spring boot启动类放在com.yjl.test包下,,再去掉@ComponentScan(basePackages = {"com.yjl.test.controller.*"}),,项目成功启动,日志中也出现:

s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/hello],methods=[GET]}" onto public java.lang.String com.yjl.test.controller.TestController.getHello()

成功运行。。。

以上是关于spring boot入门实例报404问题的主要内容,如果未能解决你的问题,请参考以下文章

Spring Boot 入门 404 错误

spring Boot学习入门篇-idea开发简单的hello world实例

Spring Boot入门

Spring Boot入门

运行spring boot应用报错:无法实例化接口org.springframework.context.ApplicationListener

如何使用 Tomcat 部署(和访问)Spring Boot 战争? (使用入门指南)