无法在春季启动中找到接口



我的服务器启动良好,但当我使用poster发送请求时,我得到一个404未找到。在控制台上我的服务器详细信息中,它说";在4毫秒内完成了Spring数据存储库扫描。找到0个JPA存储库接口"我只是想做一个简单的检查,看看是否有员工在我的MySQL数据库中。

我的存储库

package com.kingMachine.repository;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import com.kingMachine.entity.Employees;
@Repository
public interface EmployeeRepository extends JpaRepository<Employees, String>{
@Query("Select S from Employees S where S.id = ?1 and S.password = ?2")
Employees login(String id, String password);
}

我的控制器

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 com.kingMachine.entity.Employees;
import com.kingMachine.repository.EmployeeRepository;
public class EmployeeController {
@Autowired
EmployeeRepository employeeRepository;

//login info finds user by id and password in a query
@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);
}
}
}

主要应用

package com.kingMachine.login;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
@SpringBootApplication
@ComponentScan(basePackages="com.kingMachine")
public class LoginApplication {
public static void main(String[] args) {
SpringApplication.run(LoginApplication.class, args);
}
}

最后POM

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.3</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.kingMachine</groupId>
<artifactId>login</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>login</name>
<description>KingMachine Login</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>6.1.2.Final</version>
<type>pom</type>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>

指定登录应用程序注释

@SpringBootApplication(scanBasePackages = "com.kingMachine")
@EnableJpaRepositories("com.kingMachine.repository")
@EntityScan("com.kingMachine.entity")
public class LoginApplication {

并将@Controller注释添加到EmployeeController中,以便对其进行扫描

@Controller
public class EmployeeController {

因为您的主类不在存储库的父包中。您需要使用@EnableJpaRepositories。或者只需将主类移动到com.kingMachine

最新更新