【问题标题】:save data to the database using spring boot使用spring boot将数据保存到数据库
【发布时间】: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


【解决方案1】:

问题在于 json 到 java 模型的映射。

您需要以这种方式重命名您的 Model.java 属性:

 Email -> email 
 FirstName -> firstName

或添加@JsonProperty("name"):

@JsonProperty("email")
private String Email;

如果您选择属性重命名,请不要忘记 json 更改:

{
   "firstName":"jack",
   "lastName":"testjack",
   "password":"124",
   "email":"emailTest@gmail.com"
}

【讨论】:

  • 我把模型类改成:private String first_name;私人字符串姓氏;私人字符串电子邮件;私人字符串密码;
  • 请注意,在创建用户方法的服务类中,如果我在其中设置了post字段,它们将成功保存在数据库中。 @Override public UserDto createuser(UserDto user) { UserEntity userentity=new UserEntity(); BeanUtils.copyProperties(user, userentity); userentity.setEncryptedPassword("test"); userentity.setUserId("testID"); UserEntity stotedValue=userRepository.save(userentity); UserDto returnValue=new UserDto(); BeanUtils.copyProperties(stotedValue, returnValue);返回返回值; }
  • 应该是private String firstName;而不是private String first_name;
  • 您能否将所有类(UserEntityUserDto)中的 String Email 重命名为 String email(和其他文件)?
  • 谢谢,它有效。我应该总是在模型类中使用表单吗??
猜你喜欢
  • 1970-01-01
  • 2018-05-12
  • 1970-01-01
  • 1970-01-01
  • 2019-08-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多