java - PostMagging 与 spring boot java 到 mysql
问题描述
下面的 java spring boot 代码表示对 java 后端的简单 rest 调用,它在 mysql 数据库上执行插入,但是当它执行 rest 调用时,我报告了下面的错误,插入时我无法定义问题出在哪里mysql db,如何解决控制台中hibernate表示的错误?谢谢你
错误消息休眠:
"could not execute statement; SQL [n/a]; constraint [null];
nested exception is org.hibernate.exception.ConstraintViolationException:
could not execute statement
控制器:
@PostMapping("/newuser")
public User createUser(@Valid @RequestBody User userDetails) {
System.out.println("Sono nella creazione dell'utente");
return userRepository.save(userDetails);
}
模型:
@Entity
@Table(name = "users")
@EntityListeners(AuditingEntityListener.class)
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", unique = true, nullable = false)
private long id;
@Column(name = "first_name", nullable = false)
private String firstName;
@Column(name = "last_name", nullable = false)
private String lastName;
@Column(name = "email_address", nullable = true)
private String email;
/**
* Gets id.
*
* @return the id
*/
public long getId() {
return id;
}
/**
* Sets id.
*
* @param id the id
*/
public void setId(long id) {
this.id = id;
}
/**
* Gets first name.
*
* @return the first name
*/
public String getFirstName() {
return firstName;
}
/**
* Sets first name.
*
* @param firstName the first name
*/
public void setFirstName(String firstName) {
this.firstName = firstName;
}
/**
* Gets last name.
*
* @return the last name
*/
public String getLastName() {
return lastName;
}
/**
* Sets last name.
*
* @param lastName the last name
*/
public void setLastName(String lastName) {
this.lastName = lastName;
}
/**
* Gets email.
*
* @return the email
*/
public String getEmail() {
return email;
}
/**
* Sets email.
*
* @param email the email
*/
public void setEmail(String email) {
this.email = email;
}
/**
* Gets created at.
*
* @return the created at
*/
/**
* Sets created at.
*
* @param createdAt the created at
*/
/**
* Gets created by.
*
* @return the created by
*/
@Override
public String toString() {
return "User{" +
"id=" + id +
", firstName='" + firstName + '\'' +
", lastName='" + lastName + '\'' +
", email='" + email + '\'' +
'}';
}
}
存储库
import xxx.mode.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
/**
* The interface User repository.
*
* @author xxxx
*/
@Repository
public interface UserRepository extends JpaRepository<User, Long> {}
解决方案
您得到的休眠错误:
org.hibernate.exception.ConstraintViolationException
仅表示您违反了数据库约束并查看您的User实体,您有一些约束,例如firstName和lastName不应为空。因此,当下面的行执行时,您会收到这些错误:
userRepository.save(userDetails);
您确定这些字段不为空吗?
此外,您正在使用@Valid注释,但我没有看到javax.validation.constraints
您的User实体上使用了任何注释,因此,当您的 API 被调用时,实际上没有发生任何验证。我可以向你推荐的是:
在您的字段中使用
javax.validations
诸如@NotBlank(message = "First name is required" )。这将在它到达您的 API 时引发验证错误,并且不会继续userRepository.save(userDetails)
. 或者更好地为您的请求正文创建一个 DTO 类,而不是使用实体 - 这是一种代码味道,但我猜这只是为了测试目的。此外,您可以省略字段上的@Column声明,
private long id
因为它已经用@Id进行了注释。主键已经是唯一的,不能修改。
推荐阅读
- html - 如果元素有一个具有“.foo”类样式的子元素,它就像这样 [SASS]
- user-interface - 如何在 OpenEdge Progress 4GL 中创建 GUI
- java - 必需的字符串参数不存在?
- amazon-web-services - AWS Fargate 集群 - 容器内运行的 java 进程可以消耗多少任务内存?
- javascript - 是否可以将自定义属性添加到 Openlayers VectorLayer,然后在选择功能时访问它?
- android - Hilt 创建一个在 Activity 和它们的 Fragment 之间共享的视图模型实例
- c++ - 我收到此错误“在 0x7BE7B103 (msvcp140d.dll) 处引发异常:运行我的代码时
- javascript - 我在反应中遇到了一个 npm 命令错误。“找不到主模块中的错误:错误:无法解决......”
- html - 从 html 视图渲染 pdf 文件以将其作为图像预览显示在网页上
- c# - Newtonsoft 忽略序列化的属性