【发布时间】:2020-02-12 20:43:20
【问题描述】:
当我使用邮递员发布数据时,服务器回复错误代码 500。NetBeans 终端显示:(java.sql.SQLIntegrityConstraintViolationException: Column 'email' cannot be null)
在我的实体类下面:
@Entity(name="user")
public class UserEntity {
@Id
@GeneratedValue
private long id;
@Column(nullable = true)
private String userId;
@Column(nullable = true)
private String FirstName;
@Column(nullable = true)
private String LastName;
@Column(nullable = true)
private String Email;
@Column(nullable = true)
private String Password;
@Column(nullable = true)
private String encryptedPassword;
@Column()
private String emailVerificationToken;
@Column()
private Boolean emailVerificationStatus=false;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
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 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 getEncryptedPassword() {
return encryptedPassword;
}
public void setEncryptedPassword(String encryptedPassword) {
this.encryptedPassword = encryptedPassword;
}
public String getEmailVerificationToken() {
return emailVerificationToken;
}
public void setEmailVerificationToken(String emailVerificationToken) {
this.emailVerificationToken = emailVerificationToken;
}
public Boolean getEmailVerificationStatus() {
return emailVerificationStatus;
}
public void setEmailVerificationStatus(Boolean emailVerificationStatus) {
this.emailVerificationStatus = emailVerificationStatus;
}
}
下面是我的服务实现类: /* * 要更改此许可标头,请在项目属性中选择许可标头。 * 要更改此模板文件,请选择工具 |模板 * 并在编辑器中打开模板。 */ 包 com.example.mobile.demo.impl;
import com.example.mobile.demo.DTo.UserDto;
import com.example.mobile.demo.Entity.UserEntity;
import com.example.mobile.demo.repository.UserRepository;
import com.example.mobile.demo.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
*
* @author iphone
*/
@Service
public class UserserviceImpl implements UserService{
@Autowired
UserRepository userRepository;//it is in the data layer so we need the repository to save in the database
@Override
public UserDto createuser(UserDto user) {
UserEntity userentity=new UserEntity();
BeanUtils.copyProperties(user, userentity);
System.out.println("the properties has been copied to the entity");
userentity.setEncryptedPassword("test");
userentity.setUserId("testID");
System.out.println("encryptef passwird and user id has been set");
UserEntity stotedValue=userRepository.save(userentity);
UserDto returnValue=new UserDto();
BeanUtils.copyProperties(stotedValue, returnValue);
return returnValue;
}
}
我的模型类:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.example.mobile.demo.modoel;
/**
*
* @author iphone
*/
public class Model {
private String FirstName;
private String LastName;
private String Email;
private String Password;
public String getFirstName() {
return FirstName;
}
public void setFistName(String FirstName) {
this.FirstName = FirstName;
}
public String getLastName() {
return LastName;
}
public void setLastName(String LastName) {
this.LastName = LastName;
}
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;
}
}
下面是我在邮递员的 post 请求中发送的内容:
{
"FirstName":"jack",
"LastName":"testjack",
"Password":"124",
"Email":"emailTest@gmail.com"
}
【问题讨论】:
-
异常是 SQLIntegrityConstraintViolationException,这意味着在您的数据库中,您已将电子邮件列设置为不可为空。请验证您的数据库架构。
-
@ChintanPandya 你好,但是我在上面提到的 post 请求中发送 email 列的值。
-
db 中的列名是什么?如果您可以共享表架构,那就更好了。
标签: rest spring-boot netbeans postman