Home > OS >  Cannot use @NotBlank
Cannot use @NotBlank

Time:07-10

I just started working with spring and I'm trying to use @NotBlank. Problem is it is not working. In pom.xml I have this dependency:

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

The error I'm getting is this: enter image description here

This is my import:

import javax.validation.constraints.NotBlank;

and I'm also getting this error:

enter image description here

I have searched similar questions and the answer is that by adding this dependency and restarting IDE will work. In my case it's still not working. Any suggestions?

CodePudding user response:

Add a dependency on javax.validation:validation-api:2.0.1.Final. That defines the validation API (i.e. the interface) but does not implement it. spring-boot-starter-validation has a dependency on hibernate-validator which is the reference implementation of Java's validation API.

You imported the implementation but not the interface which is why your IDE was complaining. By importing both the interface and the implementation that code should compile.

  • Related