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

Spring Boot多模块项目中其他模块控制器接口无法访问如何解决?

解决Spring Boot多模块项目控制器404问题

问题原因

你启动的是login模块的MainApplication,默认情况下Spring Boot只会扫描启动类所在包及其子包下的组件,同时login模块未依赖Customer模块,导致Customer模块的CustomerController既没被引入到当前运行的应用中,也没被组件扫描到,所以访问/getCustomer会出现404。

解决方案

步骤1:让login模块依赖Customer模块

修改login模块下的pom.xml,添加对Customer模块的依赖:

<dependencies>
    <!-- 保留原有依赖 -->
    <dependency>
        <groupId>你的项目groupId</groupId>
        <artifactId>Customer</artifactId>
        <version>项目版本号</version>
    </dependency>
</dependencies>

请替换groupId、artifactId和version为你项目实际的配置(需与父pom及Customer模块pom中的配置一致)。

步骤2:调整组件扫描范围

有两种方式确保CustomerController被扫描到:

方式一:调整包结构

将MainApplication移动到一个父包下,让login和Customer模块的控制器都处于该父包的子包中。示例结构:

MultimoduleApplication
-------login
          com.example.app (MainApplication所在位置)
          com.example.app.login (LoginController所在位置)
-------Customer
          com.example.app.customer (CustomerController所在位置)

此时@SpringBootApplication会自动扫描com.example.app下的所有子包,自然包含两个模块的控制器。

方式二:在启动类上指定扫描范围

若不想调整包结构,直接在MainApplication的@SpringBootApplication注解中添加扫描路径或目标类:

@SpringBootApplication(scanBasePackages = {"login模块的包路径", "Customer模块的包路径"})
// 也可直接指定要扫描的控制器类
// @SpringBootApplication(scanBasePackageClasses = {LoginController.class, CustomerController.class})
public class MainApplication {
    public static void main(String[] args) {
        SpringApplication.run(MainApplication.class, args);
    }
}

替换为你项目实际的包路径或类名即可。

步骤3:重新构建启动

执行mvn clean install,重启login模块的MainApplication,此时访问http://localhost:8080/getCustomer就能正常获取响应。

内容的提问来源于stack exchange,提问作者Mitesh Vanaliya

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.09 07:25:04