我是 REST API 和 Spring Boot 的新手。我一直在尝试使用存储员工数据的 STS 和 MySQL 创建 REST API。
一切看起来都很好,但是当我在 Postman 中发布数据时,空行存储在 MySQL 和正文中。
//My employee class in the model
package com.example.SpringBootApplicationProject.model;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import lombok.Data;
//We need to make this simple Employee class to JPA annotation
@Data
@Entity
@Table(name="employees")
public class Employee {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
@Column(name = "first_name")
private String firstName;
@Column(name = "last_name")
private String lastName;
@Column(name = "email")
private String email;
@Column(name = "salary")
private long salary;
}
//员工控制器类
package com.example.SpringBootApplicationProject.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.example.SpringBootApplicationProject.model.Employee;
import com.example.SpringBootApplicationProject.service.EmployeeService;
@RestController
@RequestMapping("/api/employees")
public class EmployeeController {
private EmployeeService employeeService;
public EmployeeController(EmployeeService employeeService) {
super();
this.employeeService = employeeService;
}
//Build employee REST API
@PostMapping
public ResponseEntity<Employee> saveEmployee(@RequestBody Employee employee){
return new ResponseEntity<Employee>(employeeService.saveEmployee(employee), HttpStatus.CREATED);
}
}
//员工存储库类
package com.example.SpringBootApplicationProject.repository;
import org.springframework.data.jpa.repository.JpaRepository;
import com.example.SpringBootApplicationProject.model.Employee;
//This is an interface.
//JPA repository automatically provides @Repository
public interface EmployeeRepository extends JpaRepository<Employee, Long>{
//public interface EmployeeRepository extends JpaRepository<Employee, Long>{
//Employee save(Employee employee);
}
//员工服务班
package com.example.SpringBootApplicationProject.service;
import com.example.SpringBootApplicationProject.model.Employee;
public interface EmployeeService
{
Employee saveEmployee(Employee employee);
}
//员工服务实现
package com.example.SpringBootApplicationProject.service.impl;
import org.springframework.stereotype.Service;
import com.example.SpringBootApplicationProject.model.Employee;
import com.example.SpringBootApplicationProject.repository.EmployeeRepository;
import com.example.SpringBootApplicationProject.service.EmployeeService;
@Service
public class EmployeeServiceImpl implements EmployeeService{
private EmployeeRepository employeeRepository;
public EmployeeServiceImpl(EmployeeRepository employeeRepository) {
super();
this.employeeRepository = employeeRepository;
}
@Override
public Employee saveEmployee(Employee employee) {
return employeeRepository.save(employee);
}
}
Postman blank data[MySQL Empty DB][1]
当我概述您的代码时,我得到了下面列出的不同内容,这些内容解决了您的问题:
如果出现任何问题,请告诉我,抱歉我的英语不好。