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

Spring Boot接口异常:请求404且未扫描到JPA仓库接口

问题分析与修复方案

核心问题

  1. 404请求错误:EmployeeController未被Spring容器识别,缺少@RestController(或@Controller)注解,导致/login请求路径无法映射。
  2. JPA仓库未被扫描:控制台提示“Found 0 JPA repository interfaces”,因为Spring Data JPA默认仅扫描主应用类所在包(com.kingMachine.login)及其子包,而你的仓库位于com.kingMachine.repository,需要显式配置扫描路径。

具体修复步骤

1. 给Controller添加识别注解

修改EmployeeController,新增@RestController注解,让Spring识别这是一个控制器类:

package com.kingMachine.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController; // 新增注解

import com.kingMachine.entity.Employees;
import com.kingMachine.repository.EmployeeRepository;

@RestController // 标记为REST控制器
public class EmployeeController {

    @Autowired
    EmployeeRepository employeeRepository;
    
    @RequestMapping(value="/login",
            produces=MediaType.APPLICATION_JSON_VALUE,
            method=RequestMethod.POST
            )
    public ResponseEntity<Employees>login(@RequestBody Employees employees){
        Employees employeeLogin = employeeRepository.login(employees.getId(), employees.getPassword());
        if(employeeLogin != null) {
            return new ResponseEntity<>(employeeLogin, HttpStatus.OK);
        }else {
            return new ResponseEntity<>(HttpStatus.UNAUTHORIZED);
        }
    }
}

2. 配置JPA仓库扫描路径

修改主应用类LoginApplication,添加@EnableJpaRepositories注解指定仓库所在包:

package com.kingMachine.login;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories; // 导入依赖类

@SpringBootApplication(scanBasePackages = "com.kingMachine") // 扫描所有com.kingMachine下的组件
@EnableJpaRepositories(basePackages = "com.kingMachine.repository") // 指定JPA仓库扫描路径
public class LoginApplication {

    public static void main(String[] args) {
        SpringApplication.run(LoginApplication.class, args);
    }
}

3. 移除冲突的Hibernate依赖

你的POM中手动引入了hibernate-core 6.1.2.Final,但Spring Boot 2.7.3默认依赖Hibernate 5.6.11.Final,版本不匹配会导致潜在冲突。建议移除手动添加的该依赖:

<!-- 删除以下这段依赖 -->
<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-core</artifactId>
    <version>6.1.2.Final</version>
    <type>pom</type>
</dependency>

验证修改

  1. 重启服务器,控制台应显示“Found 1 JPA repository interfaces”,说明仓库已被成功扫描。
  2. 使用Postman发送POST请求到/login,携带JSON格式请求体(如{"id":"员工ID", "password":"密码"}),此时应正常返回响应,不再出现404错误。

内容的提问来源于stack exchange,提问作者Matthew Hinson

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.19 21:25:20