Spring Boot多模块项目启动后Controller接口无法识别求助
Spring Boot多模块项目接口无法访问问题
问题背景
搭建Spring Boot多模块项目,包含两个练习模块(后续将扩展更多模块):
application模块:实现Controller接口boot模块:作为项目启动入口
两个模块均继承自父模块,但启动项目后调用Controller接口时,Spring无法识别该接口,请求失败。
项目结构

Controller类代码

Boot启动模块代码

请求失败情况

各模块pom.xml代码
boot模块pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.onebox</groupId> <artifactId>onebox</artifactId> <version>1.0-SNAPSHOT</version> </parent> <artifactId>boot</artifactId> <properties> <maven.compiler.source>17</maven.compiler.source> <maven.compiler.target>17</maven.compiler.target> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> </project>
application模块pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.onebox</groupId> <artifactId>onebox</artifactId> <version>1.0-SNAPSHOT</version> </parent> <artifactId>application</artifactId> <properties> <maven.compiler.source>17</maven.compiler.source> <maven.compiler.target>17</maven.compiler.target> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> </project>
父模块pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.onebox</groupId> <artifactId>onebox</artifactId> <version>1.0-SNAPSHOT</version> <packaging>pom</packaging> <modules> <module>boot</module> <module>application</module> </modules> <properties> <maven.compiler.source>17</maven.compiler.source> <maven.compiler.target>17</maven.compiler.target> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>3.0.5</version> </parent> <!--<dependencies> <dependency> <groupId>org.springdoc</groupId> <artifactId>springdoc-openapi-ui</artifactId> <version>1.7.0</version> </dependency> </dependencies>--> </project>
问题原因及解决方法
核心问题1:启动模块未依赖application模块
boot模块作为启动入口,没有引入application模块的依赖,导致Spring容器无法加载到application模块中的Controller类。
解决步骤:
在boot模块的pom.xml中添加对application模块的依赖:
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- 添加application模块依赖 --> <dependency> <groupId>org.onebox</groupId> <artifactId>application</artifactId> <version>1.0-SNAPSHOT</version> </dependency> </dependencies>
核心问题2:启动类扫描范围覆盖不到Controller包
从图片可见,启动类OneboxApplication的包路径是org.onebox.boot,而Controller的包路径是org.onebox.application.controller。Spring Boot默认只会扫描启动类所在包及其子包,因此无法识别到Controller。
解决方法二选一:
- 调整启动类包路径:将启动类移到
org.onebox根包下,默认扫描所有子包。 - 指定扫描包范围:在启动类上添加注解指定扫描范围:
@SpringBootApplication(scanBasePackages = "org.onebox") public class OneboxApplication { public static void main(String[] args) { SpringApplication.run(OneboxApplication.class, args); } }
验证步骤
- 执行
mvn clean install编译整个项目 - 重启
boot模块的启动类 - 重新调用接口,确认是否正常访问
内容的提问来源于stack exchange,提问作者Mohamed Mamun
相关产品推荐
相关产品推荐

