我的 REST API 向 MySQL 和 Postman 提供空数据

问题描述 投票:0回答:1

我是 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]
java mysql spring spring-data-jpa postman
1个回答
0
投票

当我概述您的代码时,我得到了下面列出的不同内容,这些内容解决了您的问题:

  1. 我发现的一个错误是你的 json 有 secondaryName 但你的员工班级 有姓氏字段,请先更改它。
  2. 实际问题是 lombok @Data 注释,有时该注释不 工作不好导致这个问题。
  3. 对于这种情况,您可以删除@Data注释并创建getter和setter 以及手动参数化构造函数和空构造函数。
  4. 然后尝试从邮递员发送相同的请求,它会起作用。

如果出现任何问题,请告诉我,抱歉我的英语不好。

© www.soinside.com 2019 - 2024. All rights reserved.