我是 Spring Boot 新手。我正在尝试为员工管理系统创建我的第一个 CRUD 应用程序。应用程序运行成功,但是当我尝试在 Postman 中获取 POST 请求的响应时,收到 404 not found。
目前没有编译错误。
以下是我的控制器类:
package com.cts.Employee.controller;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.cts.Employee.model.EmployeeModel;
import com.cts.Employee.service.EmployeeService;
@RestController
@RequestMapping("/api/employees")
public class EmployeeController {
private EmployeeService employeeService;
public EmployeeController(EmployeeService employeeService) {
super();
this.employeeService = employeeService;
}
// build create employee REST API
@PostMapping()
public ResponseEntity<EmployeeModel> saveEmployee(@RequestBody EmployeeModel employee){
return new ResponseEntity<EmployeeModel>(employeeService.saveEmployee(employee), HttpStatus.CREATED);
}
}
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.2.7.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.cts</groupId>
<artifactId>Employee</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>Employeemanagement</name>
<description>Employee Management using Spring boot</description>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.hibernate.javax.persistence</groupId>
<artifactId>hibernate-jpa-2.1-api</artifactId>
<version>1.0.0.Final</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.33</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</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>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
application.properties 文件:
spring.datasource.url=jdbc:mysql://localhost:3306/emp
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.username=root
spring.datasource.password=Sunetra@26
# Hibernate properties
#spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
# create, create-drop
spring.jpa.hibernate.ddl-auto=update
我的模特班:
package com.cts.Employee.model;
import javax.persistence.Entity;
import javax.persistence.Column;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import lombok.Data;
@Data
@Entity
@Table(name="employees")
public class EmployeeModel {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
@Column(name = "first_name", nullable = false)
private String firstName;
@Column(name = "last_name")
private String lastName;
@Column(name="address")
private String address;
}
我的服务界面:
package com.cts.Employee.service;
import com.cts.Employee.model.EmployeeModel;
public interface EmployeeService {
EmployeeModel saveEmployee(EmployeeModel employee);
}
我的serviceimp类:
package com.cts.Employee.service.impl;
import org.springframework.stereotype.Service;
import com.cts.Employee.service.EmployeeService;
import com.cts.Employee.model.EmployeeModel;
import com.cts.Employee.repository.EmployeeRepository;
@Service
public class EmployeeServiceImpl implements EmployeeService{
private EmployeeRepository employeeRepository;
public EmployeeServiceImpl(EmployeeRepository employeeRepository) {
super();
this.employeeRepository = employeeRepository;
}
@Override
public EmployeeModel saveEmployee(EmployeeModel employee) {
return employeeRepository.save(employee);
}
}
邮递员发帖请求网址:
http://localhost:8080/api/employees
邮递员请求正文:
{
"firstName": "learning",
"lastName":"geek",
"address":"anywhere"
}
邮递员回复正文:
{
"timestamp": "2023-09-30T13:58:12.254+0000",
"status": 404,
"error": "Not Found",
"message": "No message available",
"path": "/api/employees"
}
我不明白我需要做出哪些改变才能获得期望的结果。
预期结果是:
{
"id":1,
"firstName": "learning",
"lastName":"geek",
"address":"anywhere"
}
如何解决这个问题?
您应该在控制器方法上放置
@PostMspping
注释以映射到特定端点。像这样
@PostMapping
public ResponseEntity<EmployeeModel> saveEmployee(@RequestBody EmployeeModel employee)
要了解 Spring 中的请求映射,请阅读 文档: