I'm having a problem. I try to validate the data passed to my DTO using the @Valid annotation and there is no result. In the stack trace the data is normally inserted in the database. I expected at least one bad request.
At first I'm testing only in the POST method.
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);
}
Stacktrace
Hibernate: insert into user (id, birth_date, cpf, email, name) values (null, ?, ?, ?, ?)