【问题标题】:Post request in Spring Boot Controller throws "java.sql.SQLIntegrityConstraintViolationException: Column 'columnname' cannot be null"Spring Boot Controller 中的发布请求抛出“java.sql.SQLIntegrityConstraintViolationException:列'columnname'不能为空”
【发布时间】:2020-11-14 15:30:49
【问题描述】:

我开始学习 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"
}

【问题讨论】:

    标签: java mysql spring spring-boot


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:
      1. 请在您的 POJO 中添加您的性别 Setter/Getter。这将解决您的问题。

      2. 我还可以看到您不确定您的请求正文。在这种情况下,在类级别(POJO)添加“@JsonIgnoreProperties(ignoreUnknown = true)”注解,这样未知字段就可以被忽略。

      3. 还在您的代码中定义 MediaType(消费/生产)(我的意思是说您期望用户提出什么样的请求)。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-07-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-04-01
        • 2020-07-05
        • 2021-02-09
        相关资源
        最近更新 更多