使用 spring 引导将数据保存到数据库

save data to the database using spring boot

当我post数据使用postman时,服务器回复错误代码500。NetBeans终端显示:(java.sql.SQLIntegrityConstraintViolationException: Column 'email' cannot为空)

下面是我的实体class:

@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;
}

}

下面是我的服务实现 class: /* * 要更改此许可证 header,请在项目属性中选择许可证 Headers。 * 要更改此模板文件,请选择工具 |模板 * 并在编辑器中打开模板。 */ 包裹 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;
}

}

我的模型class:

/*
* 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 请求中发送的 postman:

{
"FirstName":"jack",
"LastName":"testjack",
"Password":"124",
"Email":"emailTest@gmail.com"
}

问题出在 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"
}