按照官网的步骤建了一个springboot项目,该导入的也导入了,发现新建了一个controller怎么也访问不到,一直报404,
报错的原因是找不到对应的映射路径,即Controller没有被扫描到 ,。
郁闷至极,到网上搜的结果说的是Controller 方的位置不对,应该让启动类和Controller的包在同一级目录下,然而对我却没有效果。
后面在网上一直搜,找到了答案,我就博客上记录一下,俗话说好记性不如烂笔头,顺带也给客友们看看,方便查找:
解决方案:
如果使用@Controller和@EnableAutoConfiguration 注解还应该再加上一个注解:@ComponentScan 就可以了。@Controller和@EnableAutoConfiguration没有扫描注解的功能,而@ComponentScan是springboot专门用来扫描@Component, @Service, @Repository, @Controller等注解的注解;
总结:
使用springboot启动类配置扫描的两种注解配置方式:
1、@Controller
@EnableAutoConfiguration
@ComponentScan
2、@SpringBootApplication
@SpringBootApplication注解等价于@Configuration, @EnableAutoConfiguration 和 @ComponentScan