Spring Boot应用Postman请求404错误排查求助
问题排查与解决方案
核心问题:包扫描范围不匹配
你的主类DepartmentServiceApplication位于com.microservice.Departmentservice包下,而Controller、Service、Repository、Entity均属于com.microservice的同级子包(如com.microservice.FController、com.microservice.Service等)。Spring Boot默认仅扫描主类所在包及其子包,导致这些组件未被Spring容器加载,接口自然无法被识别。
解决方案
方案1:调整主类包路径
将主类DepartmentServiceApplication迁移至com.microservice包下,默认扫描范围即可覆盖所有业务子包,无需额外注解:
package com.microservice; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class DepartmentServiceApplication { public static void main(String[] args) { SpringApplication.run(DepartmentServiceApplication.class, args); } }
方案2:显式指定扫描范围
若不愿移动主类,在主类上明确配置扫描根包,同时覆盖JPA的实体与Repository扫描:
package com.microservice.Departmentservice; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.ComponentScan; import org.springframework.data.jpa.repository.config.EnableJpaRepositories; import org.springframework.boot.autoconfigure.domain.EntityScan; @SpringBootApplication @ComponentScan(basePackages = "com.microservice") @EnableJpaRepositories(basePackages = "com.microservice") @EntityScan(basePackages = "com.microservice.Entity") public class DepartmentServiceApplication { public static void main(String[] args) { SpringApplication.run(DepartmentServiceApplication.class, args); } }
额外验证项
- 确认Postman使用POST请求访问
/dept/,请求方式不匹配会返回404; - 检查Postman请求端口与Spring Boot启动端口一致(默认8080,若修改需对应);
- 确认
DepartmentController的@RestController、@RequestMapping("/dept")注解配置无误。
内容的提问来源于stack exchange,提问作者Ankita Shukla
相关产品推荐
相关产品推荐

