close

[Solved] Spring boot validation annotations @Valid and @NotBlank not working

Hello Guys, How are you all? Hope You all Are Fine. Today I am facing the following error Spring boot validation annotations @Valid and @NotBlank not working in Java. So Here I am Explain to you all the possible solutions here.

Without wasting your time, Let’s start This Article to Solve This Error.

How Spring boot validation annotations @Valid and @NotBlank not working Error Occurs?

Today I am facing the following error Spring boot validation annotations @Valid and @NotBlank not working in Java.

How To Solve Spring boot validation annotations @Valid and @NotBlank not working Error ?

  1. How To Solve Spring boot validation annotations @Valid and @NotBlank not working Error ?

    To Solve Spring boot validation annotations @Valid and @NotBlank not working Error 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

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

    To Solve Spring boot validation annotations @Valid and @NotBlank not working Error 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

Solution 1

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)

Solution 2

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.

Summery

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you? Thank You.

Also, Read