Spring boot validation annotations @Valid and @NotBlank not working

We Are Going To Discuss About Spring boot validation annotations @Valid and @NotBlank not working. So lets Start this Java Article.

Spring boot validation annotations @Valid and @NotBlank not working

Advertisements
  1. Spring boot validation annotations @Valid and @NotBlank not working

    If you are facing this problem in latest version of spring boot (2.3.0) make sure to add the following dependency:
    <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-validation</artifactId> </dependency>

  2. Spring boot validation annotations @Valid and @NotBlank not working

    If you are facing this problem in latest version of spring boot (2.3.0) make sure to add the following dependency:
    <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-validation</artifactId> </dependency>

Solution 1

Advertisements

If you are facing this problem in latest version of spring boot (2.3.0) make sure to add the following dependency:

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

Observation:
In earlier version of Spring Boot (1.4.7), javax.validation used to work out of the box. But, after upgrading to latest version, annotations broke. Adding the following dependency alone doesn’t work:

<dependency>
    <groupId>javax.validation</groupId>
    <artifactId>validation-api</artifactId>
</dependency>

Because this provides JSR Specification but not the implementation. You can also use hibernate-validator instead of spring-boot-starter-validation.

Original Author Deb Of This Content

Solution 2

Advertisements

For Anyone who is getting this issue with 2.0.1.Final:

In all SpringBoot versions above 2.2, Validations starter is not a part of web starter anymore

Check Notes here

So, all you have to do is add this dependency in your build.gradle/pom file

GRADLE:

implementation 'org.springframework.boot:spring-boot-starter-validation'

MAVEN

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

Original Author Thanny Of This Content

Solution 3

Advertisements

First you dont need to have @Valid annotation for those class variables in UpdatePrintContracts . You can delete them.

To trigger validation of a @Controller input, simply annotate the input argument as @Valid or @Validated:

@RequestMapping(value=PATH_PRINT_CONTRACTS, method=RequestMethod.POST)
public ResponseEntity<?> printContracts(@Valid @RequestBody  final UpdatePrintContracts updatePrintContracts) throws Exception {

Refer here for full understanding of validating models in spring boot.

And If you want to check that a string contains only specific characters, you must add anchors (^ for beginning of the string, $ for end of the string) to be sure that your pattern matches all the string.Curly brackets are only to write a quantity,

@Pattern(regexp = "^[\\p{Alnum}]{1,32}$")

Lastly i assume you have following jars in your classpath,

.validation-api.jar (contains the abstract API and the annotation scanner)

.hibernate-validator.jar (contains the concrete implementation)

Original Author surya Of This Content

Solution 4

Advertisements

I was using This dependency of validation in spring boot and didn’t work ,

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

I replaced it with spring-boot-starter-validation and it worked .

<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot- 
starter-validation -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
<version>2.4.0</version>

Original Author Arjun Gautam Of This Content

Conclusion

So This is all About This Tutorial. Hope This Tutorial Helped You. Thank You.

Also Read,

Siddharth

I am an Information Technology Engineer. I have Completed my MCA And I have 4 Year Plus Experience, I am a web developer with knowledge of multiple back-end platforms Like PHP, Node.js, Python and frontend JavaScript frameworks Like Angular, React, and Vue.

Leave a Comment