jamesyreid
jamesyreid

Reputation: 181

javax validation constraints not working in Spring Boot

I cannot get annotations such as @NotEmpty, @NotBlank and @NotNull to fire in my Spring Boot application.

I've followed this (maven) example:

https://spring.io/guides/gs/validating-form-input/

...and I don't see what I am doing wrong.

POJO:

import javax.validation.constraints.NotBlank;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class College
{    
    @NotBlank
    private String collegeCode;
.
.
.

Spring controller:

@RequestMapping(value="/addCollege", method = RequestMethod.POST) 
public String addCollege(@Valid @ModelAttribute("college") College college, BindingResult bindingResult, Model model, HttpSession session)
{
    if(bindingResult.hasErrors()) //this is never true
    {
        logger.debug("Errors when adding new college!!!");
        return "admin/colleges/addCollege";
    }
    collegeProcessor.saveCollege(college);
    return getAllColleges(model, session);
}

Screen:

<form action="#" th:action="${addOrEdit} == 'add' ? @{/addCollege} : @{/updateCollege}" th:object="${college}" method="post">

    <table class="table">

        <tr>
            <td>College Code</td>
            <td><input size="10" name="collegeCode" th:field="*{collegeCode}"/></td>
            <div id="errors" th:if="${#fields.hasErrors('collegeCode')}" th:errors="*{collegeCode}"></div>
        </tr>
.
.
.

As well as @NotBlank, I have tried @NotEmpty and @NotNull, but the same thing happens. My screen does not validate the input, and allows a college object with an empty collegeCode to be saved.

The interesting thing is if I change my POJO to use the deprecated Hibernate validator instead...

import org.hibernate.validator.constraints.NotBlank;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class College
{
    @NotBlank
    private String collegeCode;
.
.
.

...then the validation DOES fire, and the screen prevents me from saving a College object with an empty collegeCode.

Can anyone tell me why my validation doesn't work when using the javax.validation.constraints validators?

Upvotes: 18

Views: 19685

Answers (6)

Tiago Medici
Tiago Medici

Reputation: 2194

I m using spring boot 2.3.12 the contract first api is generate with swagger 3.0.0:

that's an attribute generated:

 @ApiModelProperty(required = true, value = "")
  @NotNull
  public String getEmail() {
    return email;
  }

here are the dependencies i used :

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.12.RELEASE</version>
        <relativePath />
    </parent>

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

    <!-- Validation API support -->
    <dependency>
        <groupId>javax.validation</groupId>
        <artifactId>validation-api</artifactId>
    </dependency>

</dependencies>

Upvotes: 1

Goce
Goce

Reputation: 491

Since version 2.3.0.RELEASE the validation starter is not included in web/webflux starters by default, you need to add it manually. More details here

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

Upvotes: 38

ukchaudhary
ukchaudhary

Reputation: 387

Include Maven dependency for 2.3.0.RELEASE in pom.xml, however, it is not required for lower version of Spring Boot Application.

<!-- https://mvnrepository.com/artifact/javax.validation/validation-api -->
<dependency>
  <groupId>javax.validation</groupId>
  <artifactId>validation-api</artifactId>
</dependency>

Upvotes: 7

Den B
Den B

Reputation: 921

I guess I've figured out this problem. I had the same problem and tried to lower spring boot version. I lowered my spring boot version from 2.3.0.RELEASE to 2.2.7.RELEASE and it works) Maybe this answer will help someone.

Upvotes: 2

Sigmar Muuga
Sigmar Muuga

Reputation: 146

having exactly the same issue, no idea what to do. All needed classes are in the classpath, no jar hells etc.

Upvotes: 0

maslan
maslan

Reputation: 2178

Please add a bean MethodValidationPostProcessor to your context

 @Bean
public MethodValidationPostProcessor methodValidationPostProcessor() {
    return new MethodValidationPostProcessor();
}

Upvotes: 0

Related Questions