关于使用Spring Boot读取请求体的问题

huangapple go评论69阅读模式
英文:

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,但没有任何运气,出现相同的错误。

关于使用Spring Boot读取请求体的问题
关于使用Spring Boot读取请求体的问题

任何指导都将不胜感激。

英文:

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 =&gt; 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=&quot;employees&quot;)
public class Employee {
	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	private long id;
	
	@Column(name = &quot;first_name&quot;, nullable = false)
	private String firstName;
	
	@Column(name = &quot;last_name&quot;)
	private String lastName;
	
	@Column(name = &quot;email&quot;)
	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(&quot;/api/employees&quot;)
public class EmployeeController {
	
	private EmployeeService employeeService;

	public EmployeeController(EmployeeService employeeService) {
		super();
		this.employeeService = employeeService;
	}
	
	@PostMapping()
	public ResponseEntity&lt;Employee&gt; saveEmployee(@RequestBody Employee employee) {
		System.out.println(&quot;@RequestBody =&gt; &quot; + employee.toString());
		return new ResponseEntity&lt;Employee&gt;(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 &quot;Accept: application/json&quot; -H &quot;Content-Type:application/json&quot; -X POST --data &quot;{\&quot;firstName\&quot;: \&quot;john\&quot;, \&quot;lastName\&quot;: \&quot;cena\&quot;}&quot; &quot;http://localhost:8080/api/employees&quot;

From the println statement, we can clearly see field values(firstName, lastName and email) read as null using @RequestBody

@RequestBody =&gt; 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.

关于使用Spring Boot读取请求体的问题
关于使用Spring Boot读取请求体的问题

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.

huangapple
  • 本文由 发表于 2023年5月14日 03:16:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/76244498.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定