52

After developing Spring roo project, I found following errors in class:

The import javax.validation.constraints.NotNull cannot be resolved

NotNull cannot be resolved to a type

I am using STS 3.1.0.RELEASE

How can this be rectified?

starball
  • 20,030
  • 7
  • 43
  • 238
Simpanoz
  • 2,729
  • 10
  • 43
  • 64

14 Answers14

113

I had the same problem. I found out that the recent versions of Spring Boot needs the separate dependency for Validation. I Tried adding below dependency in pom.xml file and it worked.

<dependency> 
    <groupId>org.springframework.boot</groupId> 
    <artifactId>spring-boot-starter-validation</artifactId> 
</dependency>
Rohit Chaudhary
  • 1,131
  • 2
  • 4
  • 3
38

The jar containing this class must be added to the build path of your project: http://mvnrepository.com/artifact/javax.validation/validation-api/1.0.0.GA

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
22

for JDK-9 the old version of "javax.validation" is not supporting. So we should add latest version.

we will know the latest version of any jar the following way

C:\Users\username\.m2\repository\javax\validation\validation-api

The above folder should have all versions of the jar, then you can add the latest version as dependency in the pom.xml file the following way

In my case "2.0.0.final" is the latest version.

<dependency>
    <groupId>javax.validation</groupId>
    <artifactId>validation-api</artifactId>
    <version>2.0.0.Final</version>
</dependency>
Kona Suresh
  • 1,836
  • 1
  • 15
  • 25
16

from Spring boot 2.3 version you have to add this dependency in your pom.xml

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

the web starters no longer contains the validation so you have to add it manually

Hafaiedh Chedly
  • 179
  • 1
  • 6
8

Im using JDK JAVA 8 and i still had a similar problem. The error disappeared just by adding the following Sample in my pom.xml

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

The following artical helped me find the solution: "https://www.baeldung.com/javax-validation"

mtaDev
  • 99
  • 1
  • 3
7

If you are using SpringBoot and you are migrating to Spring Boot 3, you need to replace all your references of javax.validation with jakarta.validation.

Check the release notes of Spring Boot:

https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-3.0-Release-Notes

Esteban
  • 1,496
  • 17
  • 22
1

In addition to copying and pasting .jar en <root-project>/lib you also have to add them in Project Structure > Libraries > Classes

GL

Hibernate Validator

Braian Coronel
  • 22,105
  • 4
  • 57
  • 62
1

Before, Gradle included this dependency by default. I don't know what happen. You have to add manually this dependence: implementation 'jakarta.validation:jakarta.validation-api:2.0.2'

asakopako
  • 11
  • 1
1

I was dealing with same problem.

Here you need to import like this - import jakarta.validation.constraints.NotNull;

Instead of - import javax.validation.constraints.NotNull

and also add the following dependency in your pom.xml file.

<dependency>
        <groupId>org.hibernate.validator</groupId>
        <artifactId>hibernate-validator</artifactId>
        <version>8.0.0.Final</version>
</dependency>
0

I had the same error and I simply downloaded API from : https://mvnrepository.com/artifact/javax.validation/validation-api/1.0.0.GA : https://mvnrepository.com/artifact/javax.validation/com.springsource.javax.validation/1.0.0.GA

It started to work -))))))))

Mary
  • 5
  • 5
0

Intellij solution: After pasting the code below you need to close intellij (entirely) then open again.

pasted the following:


    <dependency>
         <groupId>javax.validation</groupId>
         <artifactId>validation-api</artifactId>
         <version>1.0.0.GA</version>
    </dependency>

Balázs Patai
  • 105
  • 1
  • 9
0
<dependency>
    <groupId>javax.validation</groupId>
    <artifactId>validation-api</artifactId>
    <version>1.0.0.GA</version>
</dependency>

<dependency>
    <groupId>jakarta.validation</groupId>
    <artifactId>jakarta.validation-api</artifactId>
    <version>2.0.2</version>
</dependency>

there are also following versions there.

Oleksii Kyslytsyn
  • 2,458
  • 2
  • 27
  • 43
0

If you are using gradle, in "build.gradle" file add the following in the "dependencies"

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

testImplementation 'org.springframework.boot:spring-boot-starter-test'
Elikill58
  • 4,050
  • 24
  • 23
  • 45
vignesh
  • 1
  • 2
0

You might want to use Jakarta validation instead of javax. As the latter is considered legacy already. Spring Boot 3 includes Jakarta lib by default.

homiak
  • 401
  • 5
  • 12