多模块Gradle Spring项目请求404错误,模块依赖配置求助
我在搭建Java+Gradle多模块项目时,调用/tasks/hello接口一直返回404错误,Postman返回内容如下:
{ "status": 404, "error": "Not Found", "message": "No message available", "path": "/tasks/hello" }
执行./gradlew :task-service:bootRun启动项目后,日志显示“Tomcat started on port(s): 8080 (http) with context path''”,看似启动正常,但请求始终失败。
项目结构为根项目nyuro-backend(group为com.nyuro),包含两个子模块:
nyuro-backend (root-project) *domain *task-service
domain模块存放Task等实体类,task-service是Spring Boot应用模块,包含@Controller、@Service、@Repository等业务组件。已在settings.gradle中引入模块,task-service也配置了对domain的依赖,但怀疑模块导入或Spring配置存在问题,调整过@EntityScan、@EnableJpaRepositories、@ComponentScan的路径仍未解决,请求协助排查配置问题。
配置文件详情
settings.gradle(根项目)
rootProject.name = 'nyuro-backend' include 'domain' include 'task-service'
build.gradle(根项目)
plugins { id 'java' id 'application' } group 'com.nyuro' version '1.0-SNAPSHOT' repositories { mavenCentral() } dependencies { testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.1' testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.1' } test { useJUnitPlatform() }
build.gradle(domain模块)
plugins { id 'java' id 'application' } group 'com.nyuro' version '1.0-SNAPSHOT' repositories { mavenCentral() } dependencies { testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.1' testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.1' implementation 'jakarta.persistence:jakarta.persistence-api:3.1.0' } test { useJUnitPlatform() }
build.gradle(task-service模块)
plugins { id 'java' id 'application' id 'org.springframework.boot' version '3.1.0' id 'io.spring.dependency-management' version '1.1.0' } group = 'com.nyuro' version = '0.0.1-SNAPSHOT' repositories { mavenCentral() } dependencies { implementation 'org.springframework.boot:spring-boot-starter-web' implementation 'org.springframework.boot:spring-boot-starter-data-jpa' developmentOnly 'org.springframework.boot:spring-boot-devtools' testImplementation 'org.springframework.boot:spring-boot-starter-test' runtimeOnly 'com.mysql:mysql-connector-j' implementation 'org.springframework.boot:spring-boot-starter-actuator' implementation project(':domain') } tasks.named('test') { useJUnitPlatform() }
TaskServiceApplication.java
@EntityScan({"com.nyuro.domain"}) @EnableJpaRepositories({"com.nyuro.domain"}) @ComponentScan({"com.nyuro.domain"}) @SpringBootApplication public class TaskServiceApplication { public static void main(String[] args) { SpringApplication.run(TaskServiceApplication.class, args); } }
问题根源与解决办法
核心问题
你的TaskServiceApplication中@ComponentScan({"com.nyuro.domain"})仅指定扫描domain模块的组件,但task-service模块内的@Controller、@Service等业务组件(比如处理/tasks/hello的控制器)完全没被Spring扫描到,导致接口映射不存在,自然返回404。
另外,@SpringBootApplication默认会扫描当前类所在包及其子包,但你手动指定@ComponentScan后覆盖了这个默认行为,直接忽略了task-service自身的组件。
修复步骤
调整启动类注解配置
修改@SpringBootApplication的扫描范围,同时覆盖domain和task-service模块的包路径,或者保留默认扫描逻辑:@EntityScan(basePackages = "com.nyuro.domain") // 若Repository在domain模块则保留此注解,否则替换为对应包路径 @EnableJpaRepositories(basePackages = "com.nyuro.domain") // 让Spring扫描com.nyuro下所有子包,包含两个模块的组件 @SpringBootApplication(scanBasePackages = "com.nyuro") public class TaskServiceApplication { public static void main(String[] args) { SpringApplication.run(TaskServiceApplication.class, args); } }更简洁的方式:如果
TaskServiceApplication所在包为com.nyuro,直接去掉@ComponentScan,@SpringBootApplication会自动扫描com.nyuro下所有子包,此时仅需保留@EntityScan和@EnableJpaRepositories(若实体类/Repository在domain模块)。验证控制器配置
确保处理/tasks/hello的控制器类标注了@RestController或@Controller,且路径注解配置正确:@RestController @RequestMapping("/tasks") public class TaskController { @GetMapping("/hello") public String hello() { return "Hello from task service!"; } }清理重建项目
执行./gradlew clean build清理旧构建缓存,再重新启动项目./gradlew :task-service:bootRun,避免缓存导致的配置不生效问题。确认依赖引入正常
执行./gradlew :task-service:dependencies查看依赖树,确认domain模块已被正确引入到task-service中。
内容的提问来源于stack exchange,提问作者rafnakb

