Simpanoz
Simpanoz

Reputation: 2859

Error in Java Import statement "The import javax.validation.constraints.NotNull cannot be resolved"

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?

Upvotes: 63

Views: 166540

Answers (15)

Subin_Learner
Subin_Learner

Reputation: 377

I had the similar problem, I added the following dependency org.springframework.boot:spring-boot-starter-validation but still had the same error. Then found that there has been change due to a migration from javax to jakarta You need to change your imports from

javax.validation.constraints.NotNull;

to

import jakarta.validation.constraints.NotNull;

Upvotes: 2

Rohit Chaudhary
Rohit Chaudhary

Reputation: 1281

I had the same problem. I found out that the recent versions of Spring Boot need a separate dependency for validation. I tried adding the below dependency in the pom.xml file and it worked.

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

Upvotes: 128

Manu Chahar
Manu Chahar

Reputation: 21

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>

Upvotes: 2

homiak
homiak

Reputation: 471

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.

Upvotes: 1

Esteban
Esteban

Reputation: 1716

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

Upvotes: 13

vignesh
vignesh

Reputation: 11

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'

Upvotes: 1

Oleksii Kyslytsyn
Oleksii Kyslytsyn

Reputation: 2426

<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.

Upvotes: 0

Bal&#225;zs Patai
Bal&#225;zs Patai

Reputation: 105

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>

Upvotes: 0

Hafaiedh Chedly
Hafaiedh Chedly

Reputation: 179

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

Upvotes: 16

mtaDev
mtaDev

Reputation: 99

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"

Upvotes: 8

asakopako
asakopako

Reputation: 11

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'

Upvotes: 1

Mary
Mary

Reputation: 5


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 -))))))))

Upvotes: 0

Braian Coronel
Braian Coronel

Reputation: 22905

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

Upvotes: 1

Kona Suresh
Kona Suresh

Reputation: 1854

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>

Upvotes: 22

JB Nizet
JB Nizet

Reputation: 692181

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

Upvotes: 40

Related Questions