3

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, ?, ?, ?, ?)

1 Answers1

2

I ended up finding that removing a dependency from my pom.xml solved the problem.

Although I haven't imported this dependency into my project. It was causing some conflict and didn't give me any error in stacktrace.

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-validator</artifactId>
    <version>7.0.1.Final</version>
</dependency>
ZygD
  • 22,092
  • 39
  • 79
  • 102