gpt4 book ai didi

java - Spring Boot - 验证不适用于使用 DTO 的 POST

转载 作者:行者123 更新时间:2023-12-05 04:37:56 27 4
gpt4 key购买 nike

<分区>

我遇到了问题。我尝试使用 @Valid 注释验证传递给我的 DTO 的数据,但没有结果。在堆栈跟踪中,数据通常插入到数据库中。我预计至少有一个错误的请求。

起初我只在 POST 方法中进行测试。

Controller

    @PostMapping
public ResponseEntity<UserResponse> createUser(@Valid @RequestBody UserForm dto) {
User userToAdd = dto.dtoToUser();
userService.createUser(userToAdd);
return new ResponseEntity<>(UserResponse.convertToDto(userToAdd), HttpStatus.CREATED);

}

DTO

public class UserForm {

@NotBlank
private String name;
@CPF
private String cpf;
@Email
private String email;
@NotBlank
private LocalDate birthDate;

public UserForm(String name, String cpf, String email, LocalDate birthDate) {
this.name = name;
this.cpf = cpf;
this.email = email;
this.birthDate = birthDate;
}

public User dtoToUser() {
return new User(this.name, this.email, this.cpf, this.birthDate);
}

堆栈跟踪

Hibernate: insert into user (id, birth_date, cpf, email, name) values (null, ?, ?, ?, ?)

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com