我有一个用@RestController
注解的类Resource
,但它只在主类App
用@注解时使用SpringBootApplication
在顶层包中。
作品:
com
+- test
+- project
+- App.java
|
+- resources
| +- Resource.java
不起作用:
com
+- test
+- project
+- app
| +- App.java
|
+- resources
| +- Resource.java
最佳答案
我假设您使用的是 @SpringBootApplication
注释。您应该知道它等同于使用@Configuration、@EnableAutoConfiguration 和@ComponentScan。
ComponentScan configures component scanning directives for use with @Configuration classes. Provides support parallel with Spring XML's element.
One of basePackageClasses(), basePackages() or its alias value() may be specified to define specific packages to scan. If specific packages are not defined scanning will occur from the package of the class with this annotation.
您使用默认设置,因此您的扫描发生在带有注释的类的包中。
您可以像以前那样移动它,也可以在@ComponentScan 中指定 basePackages。
关于java - @RestController 未找到,如果主类不在顶级包中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30593145/