Dali Hmida
Dali Hmida

Reputation: 33

I put a validation in the name attribute but it dosn't work

I follow a spring boot tutorial and I can't solve this problem because the tutorial is not detailed enough

this is the User model class code :

package com.example.user;

import javax.validation.constraints.Past;
import javax.validation.constraints.Size;
import java.util.Date;
import java.util.List;

public class User {

    private Integer id;
    @Size(min=2, message="Name should have atleast 2 characters")
    private String name;   
    private Date birthDate;

    protected User() {
    }
    public User(Integer id, String name, Date birthDate) {
        super();
        this.id = id;
        this.name = name;
        this.birthDate = birthDate;
    }

   //getters and setters
}

this is the userResource code for the post mapping method :

@PostMapping("/users")
    public ResponseEntity<Object> createUser(@Valid @RequestBody User user) {
        User savedUser = service.save(user);
        URI location = ServletUriComponentsBuilder
            .fromCurrentRequest()
            .path("/{id}")
            .buildAndExpand(savedUser.getId()).toUri();
        return ResponseEntity.created(location).build();
    }

Upvotes: 0

Views: 29

Answers (1)

Dali Hmida
Dali Hmida

Reputation: 33

I solve the problem :
The hibernate validator dependacy was missed so I add it to the POM and everything works fine

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-validator</artifactId>
    <version>6.0.12.Final</version>
</dependency>

Upvotes: 1

Related Questions