我正在開發一個獨立的 JDBC/Springboot 專案,實際上將任何資料從郵遞員發布到 MySQL 時遇到了很多麻煩。我試過到處尋找,我覺得我應該重新啟動專案。如果到目前為止有人可以除錯我的代碼以查看發生了什么,那將非常有幫助。另外,讓我知道我的 JSON 語法對于郵遞員是否正確,以及我是否應該從我的 pom.xml 檔案中添加或減去任何內容。如果我能得到任何幫助來解決這個問題,我將不勝感激,這樣我就可以繼續我的專案了。謝謝你。
這是顯示的特定錯誤: 引起:java.sql.SQLIntegrityConstraintViolationException:列'dob'不能為空
@RestController
@RequestMapping(value = "/api/students", method = {RequestMethod.GET, RequestMethod.POST})
@ResponseBody
public class StudentController {
private static final Logger LOGGER = LogManager.getLogger(StudentController.class);
private StudentService studentService;
public StudentController(StudentService studentService) {
super();
this.studentService = studentService;
}
@ExceptionHandler
@ResponseStatus(HttpStatus.BAD_REQUEST)
public void handle(Exception e) {
LOGGER.warn("Returning HTTP 400 Bad Request", e);
}
//build create Student Rest API
@PostMapping
public ResponseEntity<Student> saveStudent(@RequestBody Student student) {
return new ResponseEntity<Student>(studentService.saveStudent(student), HttpStatus.CREATED);
}
}
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;
import javax.persistence.*;
import java.util.Date;
@Data
@Entity
@Table(name = "student")
public class Student {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long studentID;
@Column(name = "parent_id")
private long parentID;
@Column(name = "email") //nullable = false, unique = true
private String email;
@Column(name = "password")
private String password;
@Column(name = "first_name")
private String firstName;
@Column(name = "last_name")
private String lastName;
@Column(name = "dob")
@JsonFormat(shape= JsonFormat.Shape.STRING, pattern="dd-MM-yyyy")
private Date dob;
@Column(name = "home_phone")
private String homePhone;
@Column(name = "mobile")
private String mobile;
@Column(name = "first_day_on_campus")
@JsonFormat(shape= JsonFormat.Shape.STRING, pattern="dd-MM-yyyy")
private Date firstDayOnCampus;
@Column(name = "grade_level")
private String gradeLevel;
@Column(name = "gpa")
private double gpa;
@Column(name = "sat_score")
private int satScore;
@Column(name = "act_score")
private int actScore;
@Column(name = "last_login_date")
@JsonFormat(shape= JsonFormat.Shape.STRING, pattern="dd-MM-yyyy")
private Date lastLoginDate;
@Column(name = "last_login_ip")
private String lastLoginIP;
public long getStudentID() {
return studentID;
}
public void setStudentID(long studentID) {
this.studentID = studentID;
}
public long getParentID() {
return parentID;
}
public void setParentID(long parentID) {
this.parentID = parentID;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
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 Date getDob() {
return dob;
}
public void setDob(Date dob) {
this.dob = dob;
}
public String getHomePhone() {
return homePhone;
}
public void setHomePhone(String homePhone) {
this.homePhone = homePhone;
}
public String getMobile() {
return mobile;
}
public void setMobile(String mobile) {
this.mobile = mobile;
}
public Date getFirstDayOnCampus() {
return firstDayOnCampus;
}
public void setFirstDayOnCampus(Date firstDayOnCampus) {
this.firstDayOnCampus = firstDayOnCampus;
}
public String getGradeLevel() {
return gradeLevel;
}
public void setGradeLevel(String gradeLevel) {
this.gradeLevel = gradeLevel;
}
public double getGpa() {
return gpa;
}
public void setGpa(double gpa) {
this.gpa = gpa;
}
public int getSatScore() {
return satScore;
}
public void setSatScore(int satScore) {
this.satScore = satScore;
}
public int getActScore() {
return actScore;
}
public void setActScore(int actScore) {
this.actScore = actScore;
}
public Date getLastLoginDate() {
return lastLoginDate;
}
public void setLastLoginDate(Date lastLoginDate) {
this.lastLoginDate = lastLoginDate;
}
public String getLastLoginIP() {
return lastLoginIP;
}
public void setLastLoginIP(String lastLoginIP) {
this.lastLoginIP = lastLoginIP;
}
}
package com.chrisportfolio.StudentManagementSystem.service.implementations;
import com.chrisportfolio.StudentManagementSystem.model.Student;
import com.chrisportfolio.StudentManagementSystem.repository.StudentRepository;
import com.chrisportfolio.StudentManagementSystem.service.StudentService;
import org.springframework.stereotype.Service;
@Service
public class StudentServiceImpl implements StudentService {
private StudentRepository studentRepository;
public StudentServiceImpl(StudentRepository studentRepository) {
super();
this.studentRepository = studentRepository;
}
@Override
public Student saveStudent(Student student) {
return studentRepository.save(student);
}
}
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.chrisportfolio</groupId>
<artifactId>StudentManagementSystem</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>StudentManagementSystem</name>
<description>will help a school manage data, communications, and scheduling</description>
<properties>
<java.version>14</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.6.4</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.3.17</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-jpa -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<version>2.6.4</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-web -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.3.17</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.17</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.17</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.data/spring-data-jpa -->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
<version>2.6.3</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-test -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.3.17</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-aop -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>5.3.17</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-orm -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>5.3.17</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.3.17</version>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
<version>8.0.28</version>
</dependency>
<!-- https://mvnrepository.com/artifact/software.amazon.awssdk/rds -->
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>rds</artifactId>
<version>2.17.152</version>
</dependency>
<!-- https://mvnrepository.com/artifact/software.aws.rds/aws-mysql-jdbc -->
<dependency>
<groupId>software.aws.rds</groupId>
<artifactId>aws-mysql-jdbc</artifactId>
<version>1.0.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.amazonaws/aws-java-sdk-rds -->
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-rds</artifactId>
<version>1.12.181</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-hibernate -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-hibernate</artifactId>
<version>1.2.9</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.hibernate.javax.persistence/hibernate-jpa-2.1-api -->
<dependency>
<groupId>org.hibernate.javax.persistence</groupId>
<artifactId>hibernate-jpa-2.1-api</artifactId>
<version>1.0.2.Final</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.hibernate.orm/hibernate-core -->
<dependency>
<groupId>org.hibernate.orm</groupId>
<artifactId>hibernate-core</artifactId>
<version>6.0.0.CR2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.teiid/teiid-hibernate-dialect -->
<dependency>
<groupId>org.teiid</groupId>
<artifactId>teiid-hibernate-dialect</artifactId>
<version>16.0.0</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>5.2.3.Final</version>
</dependency>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.transaction/javax.transaction-api -->
<dependency>
<groupId>javax.transaction</groupId>
<artifactId>javax.transaction-api</artifactId>
<version>1.3</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-aspects -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>5.3.17</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-thymeleaf -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
<version>2.6.4</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.kafka/spring-kafka -->
<dependency>
<groupId>org.springframework.kafka</groupId>
<artifactId>spring-kafka</artifactId>
<version>2.8.4</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.heroku.api/heroku-api -->
<dependency>
<groupId>com.heroku.api</groupId>
<artifactId>heroku-api</artifactId>
<version>0.45</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.heroku.sdk/heroku-deploy -->
<dependency>
<groupId>com.heroku.sdk</groupId>
<artifactId>heroku-deploy</artifactId>
<version>3.0.4</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.heroku.api/heroku-http-apache -->
<dependency>
<groupId>com.heroku.api</groupId>
<artifactId>heroku-http-apache</artifactId>
<version>0.45</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.heroku.api/heroku-json-jackson -->
<dependency>
<groupId>com.heroku.api</groupId>
<artifactId>heroku-json-jackson</artifactId>
<version>0.45</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-heroku-connector -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-heroku-connector</artifactId>
<version>2.0.9.RELEASE</version>
</dependency>
<dependency>
<groupId>org.javassist</groupId>
<artifactId>javassist</artifactId>
<version>3.27.0-GA</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.22</version>
<scope>provided</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-log4j2 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
<version>2.6.5</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-core -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.17.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-api -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.17.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-slf4j-impl -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<version>2.17.2</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.13.2.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
{
"student": {
"student_id": "578329",
"parent_id": "34950593",
"email": "[email protected]",
"password": "goodgrief72",
"first_name": "Charlie",
"last_name": "Brown",
"dob": "10-10-2008",
"home_phone": "2025550186",
"mobile": "2025559456",
"first_day_on_campus": "09-12-2021",
"grade_level": "9",
"gpa": "4.0",
"sat_score": "N/A",
"act_score": "N/A",
"last_login_date": "2022-03-01",
"last_login_ip": "153.132.246.12"
}
}
uj5u.com熱心網友回復:
我想提一些如果是我的話我會考慮做的事情。
- 我認為你不需要
@ResponseBody - 為您的資料庫模型(學生)指定建構式,因為它是休眠所必需的
- 如果您將資料庫模型與其余模型分開,這是更好的做法
- 嘗試添加問題所在,例如(日志或您面臨的例外)
uj5u.com熱心網友回復:
有幾點需要注意。
1- 根據您提供的 JSON,您從郵遞員那里發送了不正確的學生屬性。如果您比較在 Student 類中宣告的變數名稱,它們不匹配。我猜你誤解了@Column注解,這個注解是用來參考資料庫的列的。
2- 看到您的代碼后,您不必傳遞學生 ID,因為它是自動生成的。
使用此 JSON 并發出請求。
{
"parentID": "34950593",
"email": "[email protected]",
"password": "goodgrief72",
"firstName": "Charlie",
"lastName": "Brown",
"dob": "10-10-2008",
"homePhone": "2025550186",
"mobile": "2025559456",
"firstDayOnCampus": "09-12-2021",
"gradeLevel": "9",
"gpa": 4.0,
"satScore": 0,
"actScore": 0,
"lastLoginDate": "2022-03-01",
"lastLoginIP": "153.132.246.12"
}
uj5u.com熱心網友回復:
問題是您傳遞了不正確的 json 并且 spring 嘗試在資料庫中寫入具有 null 值的學生,并且資料庫對某些欄位沒有 NULL 約束。
首先你需要更正你的json:
json 中的 -keys 應該對應物體中的值名稱
- 如果你的值是 int/double 你不能傳遞字串,比如:“sat_score”:“N/A”
- 洗掉“學生”標題和末尾相應的括號
您的 Json 中的 -id 不會被使用,因為您的物體使用資料庫生成的 id
此外,您的物體類中不需要引數建構式。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/474803.html
標籤:爪哇 mysql 行家 邮差 spring-jdbc
上一篇:根據條件減去行
下一篇:如果插入不成功,避免增加主鍵?
