首页 > 解决方案 > 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"
}

标签: javamysqlspringspring-boot

解决方案


在从 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;
      }

推荐阅读