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

15

62

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?

Billyebilobate answered 30/11, 2012 at 16:21 Comment(0)
O
40

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

Outflow answered 30/11, 2012 at 16:35 Comment(2)
Then it says "Overriding managed version 1.1.0.Final for validation-api"? What do I doSesquialtera
springboot already declares this dependency. This solution didn't work in my case.Listel
N
126

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>
Nidanidaros answered 6/10, 2020 at 7:40 Comment(3)
This solved the error that I got after upgrading Spring Boot to 2.4.1 from 2.2.5.RELEASENeologism
this solves the problem, just added the dependency and refreshed maven. IntellijArdrey
I've found this error after upgrade spring boot to 3.0.2 from 2.6, solved after add this dependencyWingfield
O
40

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

Outflow answered 30/11, 2012 at 16:35 Comment(2)
Then it says "Overriding managed version 1.1.0.Final for validation-api"? What do I doSesquialtera
springboot already declares this dependency. This solution didn't work in my case.Listel
W
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>
Wellordered answered 15/12, 2017 at 9:45 Comment(2)
I updated according to my latest version and yet the problem is persistent.Ronaldronalda
New Version: 2.0.1.FinalEphebe
D
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

Deanndeanna answered 5/5, 2021 at 23:26 Comment(0)
F
13

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

Femoral answered 18/12, 2022 at 10:37 Comment(0)
L
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"

Lidia answered 14/2, 2021 at 13:44 Comment(0)
Y
2

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>
Youlandayoulton answered 20/4, 2023 at 4:18 Comment(0)
S
2

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;
Selfassured answered 23/10, 2023 at 14:34 Comment(1)
spring boot 3 , javax replaced with jakarta, this one worked for meWoolley
H
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

Horselaugh answered 14/6, 2020 at 11:41 Comment(0)
C
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'

Chemosmosis answered 3/12, 2020 at 1:45 Comment(0)
L
1

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'
Laoighis answered 5/12, 2022 at 14:50 Comment(0)
C
1

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.

Crier answered 13/4, 2023 at 11:17 Comment(0)
S
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 -))))))))

Sorcery answered 30/6, 2020 at 18:36 Comment(0)
T
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>

Tragedian answered 27/2, 2022 at 7:41 Comment(0)
R
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.

Ron answered 14/3, 2022 at 19:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.