Spring Initializr生成的demo测试404错误
作者:互联网
体验Spring Initializr生成的spring boot工程,启动成功,
目录结构如下:
添加了一个简单的controller后,启动成功但访问报404错误;
原因:
springboot默认扫描启动类当前及以下包;
- 启动类CloudDemoApplication的包路径:com.unisound.ig.cloudDemo
- controller的包路径:com.unisound.ig.controller
因此,无法识别controller所在包的Bean;
解决方案:
1、重写Springboot默认的扫描包路径,自定义扫描包路径,在启动类上添加注解
1 @SpringBootApplication 2 @ComponentScan(basePackages = {"com.unisound.ig.*"}) 3 public class CloudDemoApplication { 4 5 public static void main(String[] args) { 6 SpringApplication.run(CloudDemoApplication.class, args); 7 } 8 9 }
2、把启动类上移到com.unisound.ig.层,这样就能主动扫描controller的Bean了
参考:https://segmentfault.com/a/1190000011930454
https://blog.csdn.net/weixin_37304575/article/details/79495129
标签:unisound,demo,启动,controller,404,CloudDemoApplication,Spring,com,ig 来源: https://www.cnblogs.com/huahua035/p/10984539.html