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, ?, ?, ?, ?)
3
  • share the request body are you are passing Commented Jan 10, 2022 at 7:57
  • Please add the error stacktrace.
    – Prog_G
    Commented Jan 10, 2022 at 7:57
  • { "name":"Maurinei", "cpf":"60711829055", "email":"[email protected]", "birthDate":"2025-08-05" } That was my request. There was no error in the stacktrace, just the insertion of data. I ended up finding that removing a dependency from my pom.xml solved the problem. Commented Jan 10, 2022 at 8:13

1 Answer 1

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>

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.