英文:
Issue with reading request body using spring boot
问题
以下是你提供的内容的中文翻译:
我是新手学习Java和Springboot,我试图创建一个简单的REST API来将员工记录插入到mysql数据库中。出现以下错误:
@RequestBody => 员工 [id=0, firstName=null, lastName=null, email=null]
Servlet.service() 对于路径为空的上下文中的servlet [dispatcherServlet] 抛出异常 [请求处理失败:org.springframework.dao.DataIntegrityViolationException:not-null属性引用了null或临时值:com.saa.springboot.model.Employee.firstName],根本原因如下:
org.hibernate.PropertyValueException:not-null属性引用了null或临时值:com.saa.springboot.model.Employee.firstName
在 org.hibernate.engine.internal.Nullability.checkNullability(Nullability.java:111) ~[hibernate-core-6.1.7.Final.jar:6.1.7.Final]
Employee 类:
@Data
@Entity
@Table(name="employees")
public class Employee {
@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 = "email")
private String email;
}
EmployeeController
package com.saa.springboot.controller;
import java.util.List;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
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.saa.springboot.model.Employee;
import com.saa.springboot.service.EmployeeService;
@RestController
@RequestMapping("/api/employees")
public class EmployeeController {
private EmployeeService employeeService;
public EmployeeController(EmployeeService employeeService) {
super();
this.employeeService = employeeService;
}
@PostMapping()
public ResponseEntity<Employee> saveEmployee(@RequestBody Employee employee) {
System.out.println("@RequestBody => " + employee.toString());
return new ResponseEntity<Employee>(employeeService.saveEmployee(employee), HttpStatus.CREATED);
}
}
应用程序属性:
spring.datasource.url = jdbc:mysql://localhost:3306/ems_db?useSSL=false
spring.datasource.username = root
spring.datasource.password = pwd
spring.jpa.database-platform = org.hibernate.dialect.MySQLDialect
spring.jpa.generate-ddl = true
spring.jpa.hibernate.ddl-auto = update
Curl 请求:
curl -i -H "Accept: application/json" -H "Content-Type:application/json" -X POST --data "{"firstName": "john", "lastName": "cena"}" "http://localhost:8080/api/employees"
从 println
语句中,我们可以清楚地看到字段值(firstName、lastName 和 email)使用 @RequestBody 读取为 null。
@RequestBody => 员工 [id=0, firstName=null, lastName=null, email=null]
我不确定我漏掉了什么,我已经尝试使用 Postman,但没有任何运气,出现相同的错误。
任何指导都将不胜感激。
英文:
I am new to Java and Springboot, I am trying to create simple REST API to insert employee record into mysql database. Getting following error
@RequestBody => Employee [id=0, firstName=null, lastName=null, email=null]
Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed: org.springframework.dao.DataIntegrityViolationException: not-null property references a null or transient value : com.saa.springboot.model.Employee.firstName] with root cause
org.hibernate.PropertyValueException: not-null property references a null or transient value : com.saa.springboot.model.Employee.firstName
at org.hibernate.engine.internal.Nullability.checkNullability(Nullability.java:111) ~[hibernate-core-6.1.7.Final.jar:6.1.7.Final]
Employee Class:
@Data
@Entity
@Table(name="employees")
public class Employee {
@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 = "email")
private String email;
}
EmployeeController
package com.saa.springboot.controller;
import java.util.List;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
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.saa.springboot.model.Employee;
import com.saa.springboot.service.EmployeeService;
@RestController
@RequestMapping("/api/employees")
public class EmployeeController {
private EmployeeService employeeService;
public EmployeeController(EmployeeService employeeService) {
super();
this.employeeService = employeeService;
}
@PostMapping()
public ResponseEntity<Employee> saveEmployee(@RequestBody Employee employee) {
System.out.println("@RequestBody => " + employee.toString());
return new ResponseEntity<Employee>(employeeService.saveEmployee(employee), HttpStatus.CREATED);
}
}
Application Properties:
spring.datasource.url = jdbc:mysql://localhost:3306/ems_db?useSSL=false
spring.datasource.username = root
spring.datasource.password = pwd
spring.jpa.database-platform = org.hibernate.dialect.MySQLDialect
spring.jpa.generate-ddl = true
spring.jpa.hibernate.ddl-auto = update
Curl Request:
curl -i -H "Accept: application/json" -H "Content-Type:application/json" -X POST --data "{\"firstName\": \"john\", \"lastName\": \"cena\"}" "http://localhost:8080/api/employees"
From the println
statement, we can clearly see field values(firstName, lastName and email) read as null using @RequestBody
@RequestBody => Employee [id=0, firstName=null, lastName=null, email=null]
I am not sure what I am missing here, I have tried with Postman as well without any luck, same error.
Any guidance is must appreciated.
答案1
得分: 0
问题出在Eclipse设置上。由于Eclipse没有安装lombok库,因此Employee类上的@Data
注释无法正常工作,也无法生成getter和setter。为了解决这个问题,我在Eclipse安装文件夹中安装了lombok并重新启动了Eclipse。
英文:
The issue was with Eclipse setup. The lombok library was not installed for eclipse due to this @Data
annotation was not working on Employee class and hence no getters and setters. To solve, I installed lombok in eclipse installation folder and restarted it.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论