[英]springboot api service: "status": 404, "error": "Not Found",
I try to make an API work in springboot however when I enter the request: http://localhost:8080/employee/all I get this result:我尝试使 API 在 springboot 中工作但是当我输入请求时:http://localhost:8080/employee/all 我得到这个结果:
"timestamp": "2021-10-10T20:05:58.353+00:00",
"status": 404,
"error": "Not Found",
"path": "/employee/all"
it is a classic service with a model, a service, a repository, a mapper and an exception if there is no employee, database used is in sql with the entities presented below它是一个经典服务,带有 model、一个服务、一个存储库、一个映射器和一个异常(如果没有员工),使用的数据库在 sql 中,实体如下所示
service:服务:
package com.employee.employeemanager.service;
import com.employee.employeemanager.exception.UserNotFoundException;
import com.employee.employeemanager.model.Employee;
import com.employee.employeemanager.repo.EmployeeRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.UUID;
@Service
public class EmployeeService {
private final EmployeeRepository employeeRepository;
@Autowired
public EmployeeService(EmployeeRepository employeeRepository) {
this.employeeRepository = employeeRepository;
}
public Employee addEmployee(Employee employee){
employee.setEmployeeCode(UUID.randomUUID().toString());
return employeeRepository.save(employee);
}
public List<Employee> findAllEmployees() {
return employeeRepository.findAll();
}
public Employee updateEmployee(Employee employee) {
return employeeRepository.save(employee);
}
public Employee findEmployeeById(Long id){
return (Employee) employeeRepository.findEmployeeById(id).orElseThrow(() -> new UserNotFoundException("User by id " + id + " was not found"));
}
public void deleteEmployee(Long id){
employeeRepository.deleteEmployeeById(id);
}
}
repository存储库
package com.employee.employeemanager.repo;
import com.employee.employeemanager.model.Employee;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import java.util.Optional;
@Repository
public interface EmployeeRepository extends JpaRepository<Employee, Long> {
void deleteEmployeeById(Long id);
Optional<Employee> findEmployeeById(Long id);
}
model model
package com.employee.employeemanager.model;
import javax.persistence.*;
import java.io.Serializable;
@Entity
public class Employee implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(nullable = false, updatable = false)
private Long id;
private String name;
private String email;
private String jobTitle;
private String phone;
private String imageUrl;
@Column(nullable = false, updatable = false)
private String employeeCode;
public Employee() {
}
public Employee(String name, String email, String jobTitle, String phone, String imageUrl, String employeeCode) {
this.name = name;
this.email = email;
this.jobTitle = jobTitle;
this.phone = phone;
this.imageUrl = imageUrl;
this.employeeCode = employeeCode;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getJobTitle() {
return jobTitle;
}
public void setJobTitle(String jobTitle) {
this.jobTitle = jobTitle;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getImageUrl() {
return imageUrl;
}
public void setImageUrl(String imageUrl) {
this.imageUrl = imageUrl;
}
public String getEmployeeCode(){
return employeeCode;
}
public void setEmployeeCode(String employeeCode) {
this.employeeCode = employeeCode;
}
@Override
public String toString() {
return "Employee{" +
"id=" + id +
", name='" + name + '\'' +
", email'" + email + '\'' +
", jobTitle'" + jobTitle + '\'' +
", phone'" + phone + '\'' +
", imageUrl'" + imageUrl + '\'' +
'}';
}
}
mapper映射器
package com.employee.employeemanager.mapper;
import com.employee.employeemanager.model.Employee;
import com.employee.employeemanager.service.EmployeeService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RequestMapping
@RestController("/employee")
public class EmployeeMapper {
private final EmployeeService employeeService;
public EmployeeMapper(EmployeeService employeeService) {
this.employeeService = employeeService;
}
@GetMapping("/all")
public ResponseEntity<List<Employee>> getAllEmployees() {
List<Employee> employees = employeeService.findAllEmployees();
return new ResponseEntity<>(employees, HttpStatus.OK);
}
@GetMapping("/find/{id}")
public ResponseEntity<Employee> getAllEmployees (@PathVariable("id") Long id) throws Throwable {
Employee employees = employeeService.findEmployeeById(id);
return new ResponseEntity<>(employees, HttpStatus.OK);
}
@PostMapping("/add")
public ResponseEntity<Employee> addEmployee(@RequestBody Employee employee){
Employee newEmployee = employeeService.addEmployee(employee);
return new ResponseEntity<>(newEmployee, HttpStatus.CREATED);
}
@DeleteMapping("/delete/{id}")
public ResponseEntity<?> deleteEmployee(@PathVariable("id") Long id){
employeeService.deleteEmployee(id);
return new ResponseEntity<>(HttpStatus.OK);
}
}
exception例外
package com.employee.employeemanager.exception;
public class UserNotFoundException extends RuntimeException{
public UserNotFoundException(String message) {
super(message);
}
}
pom.xml pom.xml
<?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.5.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.employee</groupId>
<artifactId>employeemanager</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>employeemanager</name>
<description>EMployee Manager App </description>
<properties>
<java.version>1.8</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>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><configuration><source>10</source><target>10</target></configuration></plugin>
</plugins>
</build>
</project>
The problem is in the EmployeeMapper
.问题出在
EmployeeMapper
中。
This is what you did in your code:这是您在代码中所做的:
@RequestMapping
@RestController("/employee")
If you change it to:如果将其更改为:
@RequestMapping("/employee")
@RestController
It should be fixed.它应该是固定的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.