【发布时间】:2022-01-09 18:46:37
【问题描述】:
我是 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]
【问题讨论】:
-
我查看了与此类似的另一篇文章,但仍然没有找到答案。 stackoverflow.com/questions/55049763/…
-
通常,开始查找的好地方是应用程序日志文件。事实上,如果您打开 SQL 语句的日志记录,您可以看到任何与数据库交互的尝试。您还将查看是否有任何 Spring 配置错误或任何其他可能发生的错误。
-
@MattVickery 我不知道在哪里可以看到应用程序日志文件。但是我不确定这是否有帮助,在 Employee 类中,当我给出 ''' @Column(name = "first_name", nullable = false) ''' 我在 Postman 中得到 50x 错误。即使我在 POST 语句中提供数据,它也将其视为空的。
-
我明白了。我会考虑(以最强有力的方式)设置您的日志记录,以便您可以查看应用程序启动和执行时发生的情况。尝试遵循本指南:tutorialspoint.com/spring/logging_with_log4j.htm 您不需要添加任何新的日志语句,您只需要查看您的应用程序组件(Spring 框架等)必须说什么。将日志记录设置为 DEBUG 级别。
-
@FaeemazazBhanej 我以为新版本的 Spring Boot 会自动调用“Autowired”。
标签: java mysql spring spring-data-jpa postman