java - Spring Boot Controller 中的发布请求抛出“java.sql.SQLIntegrityConstraintViolationException:列'columnname'不能为空”
问题描述
我开始学习 Spring Boot,目前正在尝试编写我的第一个 API。用于创建新课程的控制器端点工作得很好(通过 Postman 发送 JSON 对象)。但是,我创建新申请人的端点返回
{
"timestamp": "2020-11-14T15:21:47.189+00:00",
"status": 500,
"error": "Internal Server Error",
"message": "",
"path": "/demo/addapplicant"
}
在邮递员和
java.sql.SQLIntegrityConstraintViolationException:列“性别”不能为空
在终端窗口中。当我更改我的 MySQL 数据库以允许 Nulls 用于性别时,它只会为不同的属性提供相同的异常。我假设 JSON 未正确转换为 Java 对象。但由于它适用于课程,我不确定是什么导致了这个问题。
申请人类别:
package com.example.demoSql;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import java.util.Date;
@Entity
public class Applicant {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Integer id;
private String firstname;
private String lastname;
private String gender; //only m, w, d allowed
private Date birthdate;
private String city;
private Integer zip_code;
private String street;
private String housenumber;
private Float highschool_grade;
private String highschool_certificate;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getFirstname() {
return firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
public String getLastname() {
return lastname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
public String gender() {
return gender;
}
public void gender(String gender) {
this.gender = gender;
}
public Date getBirthdate() {
return birthdate;
}
public void setBirthdate(Date birthdate) {
this.birthdate = birthdate;
}
public String city() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public Integer getZipCode() {
return zip_code;
}
public void setZipCode(Integer zip_code) {
this.zip_code = zip_code;
}
public String getStreet() {
return street;
}
public void setStreet(String street) {
this.street = street;
}
public String getHousenumber() {
return housenumber;
}
public void setHousenumber(String housenumber) {
this.housenumber = housenumber;
}
public Float getHighschoolGrade() {
return highschool_grade;
}
public void setHighschoolGrade(Float highschool_grade) {
this.highschool_grade = highschool_grade;
}
public String getHighschoolCertificate() {
return highschool_certificate;
}
public void setHighschoolCertificate(String highschool_certificate) {
this.highschool_certificate = highschool_certificate;
}
}
控制器:
package com.example.demoSql;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
@RequestMapping(path="/demo")
public class MainController {
@Autowired
private CourseRepository courseRepository;
@Autowired
private ApplicantRepository applicantRepository;
@PostMapping(path="/addcourse")
public @ResponseBody String addNewCourse (@RequestBody Course course) {
courseRepository.save(course);
return "Saved";
}
@GetMapping(path="/allcourses")
public @ResponseBody Iterable<Course> getAllUsers() {
// This returns a JSON or XML with the courses
return courseRepository.findAll();
}
@PostMapping(path="/addapplicant")
public @ResponseBody String addNewApplicant (@RequestBody Applicant applicant) {
applicantRepository.save(applicant);
return "Saved";
}
}
JSON 对象:
{
"firstname": "Erik",
"lastname": "Bosse",
"gender": "m",
"city": "Stuttgart",
"zip_code": 753924,
"street": "Hauptstraße",
"housenumber": "24",
"highschool_grade": 1.5,
"highschool_certificate": "C://dshfjhsdnsdklgdsl"
}
解决方案
在从 json 映射到 java Obejct (Applicant) 后,性别设置为 null,因为您在 "Applicant" 中没有性别 getter/setter:
你需要使用:
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
插入:
public String gender() {
return gender;
}
public void gender(String gender) {
this.gender = gender;
}
推荐阅读
- javascript - 太多的事件监听器正在减慢点击响应
- node.js - 访问节点 pdf-poppler 中转换后的文件名
- elixir - 在 Ecto Elixir 中分页 union_all 查询
- django - django db ProgrammingError
- object-detection-api - Google Colab 上的 TF2 突然比平时慢
- qt - 如何将qt6安装或更新到centos 8?
- spring - 使用 Hikari CP 和 JDBC 的 Oracle CHAR 列和尾随空格
- formatting - 有没有更好的方法来打破 Xtend 模板中的长行?
- bash - 按特定分隔符拆分字符串并提取输出以进行进一步处理
- signals - gnuradio:如何更改源的带宽?