You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Boot多模块项目跨模块导入编译构建失败问题

Spring Boot多模块构建与API 404问题解决方案

一、编译报错(找不到common-service包)处理

  • 检查common-service基础配置:确认其<packaging>为jar,src/main/java下的包路径与依赖引用完全一致(比如com.hh.sukku.common.beans必须存在),同时保证maven-compiler-plugin的JDK版本和account-service统一,避免版本不兼容导致编译产物无法识别。
  • 校验account-service依赖配置:确保依赖的groupId、artifactId、version与common-service完全匹配,且未设置provided/test这类限制编译时引用的scope。正确配置示例:
<dependency>
    <groupId>com.hh.sukku</groupId>
    <artifactId>common-service</artifactId>
    <version>${project.version}</version>
</dependency>
  • 强制按依赖顺序构建:先单独执行mvn clean install -DskipTests构建common-service,确保它被安装到本地Maven仓库,再构建account-service。父pom的<modules>需明确指定模块顺序,避免Maven自动排序出错:
<modules>
    <module>common-service</module>
    <module>account-service</module>
    <module>product-service</module>
    <module>auth-service</module>
    <module>application-service</module>
</modules>
  • 清理Maven本地缓存:删除本地仓库中com/hh/sukku/common-service目录,重新构建,避免缓存损坏的包引发问题。

二、API返回404问题排查

  • 检查启动类扫描范围:application-service的启动类需覆盖所有子模块的Controller包,若启动类所在包层级过高,需手动添加@ComponentScan指定扫描路径:
@SpringBootApplication
@ComponentScan(basePackages = {"com.hh.sukku"})
public class ApplicationServiceApplication {
    public static void main(String[] args) {
        SpringApplication.run(ApplicationServiceApplication.class, args);
    }
}
  • 验证Controller注解:确认Controller类标注@RestController,API方法的@GetMapping/@PostMapping等注解路径无拼写错误。
  • 确认application-service依赖:需引入所有业务子模块(account-service、product-service等)的依赖,否则对应Controller无法被Spring容器加载。
  • 查看启动日志:检查启动日志中是否有Controller路径映射的记录,确认接口是否被正确注册。

内容的提问来源于stack exchange,提问作者Arun Sudhakaran

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.25 21:57:31