java - @RestController 未找到,如果主类不在顶级包中

标签 java spring spring-boot

我有一个用@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。

来自documentation

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/

相关文章:

java - 如何在 Java 中使用 xpath 查找 xml 中的节点值或属性并将其替换为另一个值?

java - spring 应用程序如何在内部调用另一个普通的 servlet 但托管在同一个 tomcat 上

java - Spring @SessionAttributes 返回 null

java - 为 Spring Data/hibernate 中的实体获取异常

spring-boot - 是否可以为单个 Spring Rest 端点自定义 ObjectMapper?

java - 如何在不删除整个实体的情况下删除外键

java - 使用 Maven 配置在 Eclipse 中为 Hadoop 作业设置 Java 堆空间

java - eToken java key 工具证书

java - 快速排序-枢轴选择策略如何影响快速排序的整体Big-oh行为?

java - springboot 2.0.5集成websocket错误