Spring Boot中GET/POST API返回404问题排查求助
接口持续返回404错误排查求助
我正在搭建首个全栈网站,从数据库入手开发API,但调用GET和POST接口时持续返回404错误,已尝试多种方法仍无头绪。我确认包结构正确、注解齐全,可能忽略了某些细节,附上相关代码请求帮助排查问题。

相关代码
主类代码
@SpringBootApplication @ComponentScan(basePackages = "com.vault.safe.MyModel.UserModel", ) @EnableJpaRepositories("com.vault.safe.repositories.UserRepository") public class DigitalVaultApplication { public static void main(String[] args) { SpringApplication.run(DigitalVaultApplication.class, args); } }
用户控制器代码
@RestController @RequestMapping("users") public class UserController { @Autowired private UserService userService; //************************************************************************************* //Constructors, Getters, and Setters public UserController(UserService userService){ super(); this.userService = userService; } public UserService getUserService(){ return userService; } public void setUserService(UserService userService){ this.userService = userService; } //************************************************************************************* @PostMapping("/add") public UserModel addUser(@RequestBody UserModel userModel){ return userService.addUser(userModel); } @GetMapping("/getUserById/{userId}") public UserModel getUserById(@PathVariable Long userId){ return userService.findById(userId); } }
用户服务代码
@Service public class UserService { @Autowired private UserRepository userRepository; public UserService(UserRepository userRepository) { super(); this.userRepository = userRepository; } public UserRepository getUserRepository(){ return userRepository; } public void setUserRepository(UserRepository userRepository) { this.userRepository = userRepository; } //*********************************************************************************************************** public UserModel addUser(UserModel User){ return userRepository.save(User); } public UserModel findById(Long userId) { Optional<UserModel> userOptional = userRepository.findById(userId); if (userOptional.isPresent()) { return userOptional.get(); } else { throw new UserNotFoundException("User not found with ID: " + userId); } } }
模型类构造方法
public UserModel(long id, String username, String email, String password, String password2, String firstName, String lastName, Integer age, String street1, String street2, String city, String state, String country, String zipcode, String phone) { this.id = id; this.username = username; this.email = email; this.password = password; this.password2 = password2; this.firstName = firstName; this.lastName = lastName; this.age = age; this.street1 = street1; this.street2 = street2; this.city = city; this.state = state; this.country = country; this.zipcode = zipcode; this.phone = phone; }
仓库接口代码
@Repository public interface UserRepository extends JpaRepository<UserModel, Long>{ List<UserModel> findUserById(Long id); }
build.gradle依赖配置
dependencies { implementation 'org.springframework.boot:spring-boot-starter-data-jdbc' implementation 'org.springframework.boot:spring-boot-starter-jdbc' implementation 'org.springframework.cloud:spring-cloud-starter-config' implementation 'org.springframework.cloud:spring-cloud-starter-gateway' implementation 'javax.persistence:persistence-api:1.0' implementation('org.springframework.boot:spring-boot-starter-data-jpa') { exclude group: 'org.springframework.boot', module: 'spring-boot-starter-web' } implementation 'org.springframework:spring-context' implementation 'com.sun.jersey.contribs:jersey-apache-client4:1.19.4' runtimeOnly 'com.mysql:mysql-connector-j' testImplementation 'org.springframework.boot:spring-boot-starter-test' developmentOnly 'org.springframework.boot:spring-boot-devtools' }
application.properties配置
server.port = 8080 spring.config.import=optional:configserver:http://localhost:8888 spring.datasource.url=jdbc:mysql://localhost:3306/user_info?currentSchema=safetybox spring.datasource.username=root spring.datasource.password=Example spring.jpa.show-sql=true spring.jpa.hibernate.ddl-auto=update spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQLDialect spring.main.web-application-type=reactive
问题排查与修复方案
1. 核心依赖冲突与缺失
- 问题:在
spring-boot-starter-data-jpa中排除了spring-boot-starter-web,同时配置spring.main.web-application-type=reactive但未引入spring-boot-starter-webflux,导致既无Servlet MVC环境,也无Reactive WebFlux环境,控制器无法被识别映射。 - 修复:
- 若用传统Servlet栈:去掉
spring-boot-starter-data-jpa中的exclude配置,添加implementation 'org.springframework.boot:spring-boot-starter-web',删除spring.main.web-application-type=reactive。 - 若用Reactive栈:添加
implementation 'org.springframework.boot:spring-boot-starter-webflux',保留spring.main.web-application-type=reactive,同时将控制器返回类型改为Mono<UserModel>适配响应式编程。
- 若用传统Servlet栈:去掉
2. 组件扫描路径错误
- 问题:
@ComponentScan指定的basePackages是类的全路径com.vault.safe.MyModel.UserModel,而非包路径,导致控制器、服务等组件无法被Spring扫描加载。 - 修复:将
basePackages修改为根包路径(如com.vault.safe),或直接删除@ComponentScan——@SpringBootApplication默认会扫描主类所在包及其子包。
3. 仓库接口冗余方法
- 问题:
UserRepository中定义的findUserById(Long id)返回List<UserModel>,但JpaRepository已提供findById(Long id)返回Optional<UserModel>,与服务层逻辑不匹配,易造成混淆。 - 修复:删除
findUserById(Long id)方法,直接使用父接口提供的findById方法。
4. 注入方式冗余
- 问题:
UserController和UserService同时使用构造器注入和@Autowired注解,Spring 4.3+之后构造器注入无需额外添加@Autowired,属于冗余配置。 - 修复:删除类中的
@Autowired注解,保留构造器注入即可。
5. 不必要依赖清理
- 删除
spring-boot-starter-data-jdbc和spring-boot-starter-jdbc(与JPA冲突,二选一即可)。 - 删除
javax.persistence:persistence-api(spring-boot-starter-data-jpa已包含相关API)。 - 若无需网关功能,删除
spring-cloud-starter-gateway依赖。
内容的提问来源于stack exchange,提问作者CodingApperentice
相关产品推荐
相关产品推荐

