vuld0
vuld0

Reputation: 334

@NotBlank, @NonNull not validating for the string "name"

I am a newbie in Spring-boot and was learning to validate constraints, here the @NotBlank, @NonNull doesn't seem to work. What I want is to provide bad request for the null value given. Am I missing something here?

The snippet of model class has been given below:

Person.java

public class Person {
    private final UUID id;
    @NotBlank
    private final String name;

    public Person(@JsonProperty("id") UUID id,
                  @JsonProperty("name") String name){
        this.id = id;
        this.name = name;

    }

And the controller class is given below:

PersonController.java

@PostMapping
    public void addPerson(@Valid @NonNull @RequestBody Person person) {
        personService.addPerson(person);
    }

pom.xml

<dependency>
            <groupId>javax.validation</groupId>
            <artifactId>validation-api</artifactId>
            <version>2.0.1.Final</version>
</dependency>

Upvotes: 1

Views: 1296

Answers (1)

vuld0
vuld0

Reputation: 334

Turns out the problem was in the pom.xml file.

About the validation part: The Validation Starter dependency is no longer included in the web starter dependencies (source: [check][1])

Select it when creating the Spring Boot config or just add at the pom.xml:

<dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-validation</artifactId>
</dependency>

[1]: import javax.validation.constraints.NotEmpty; not working)

Upvotes: 2

Related Questions