Haidepzai
Haidepzai

Reputation: 1070

Spring: java: package javax.validation.constraints does not exist

I already have this dependency in my pom.xml file

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

And have refreshed Maven and already restarted IntelliJ but always get this error java: package javax.validation.constraints does not exist

It cannot find this package for some reason. But this error occurs only when I try to compile.

In my Entity class I imported import javax.validation.constraints.Size;

and want to add an annotation to a column for example:

@Size(min = 1)
private String name;

or

@DecimalMin("0.01")
private BigDecimal amount;

But always I get the problem with the validation package.

There is no error message in the IDE, the error appears when I run.

Upvotes: 53

Views: 113688

Answers (5)

SSK
SSK

Reputation: 3766

You need to change your imports from

javax.validation.constraints.NotNull;

to

import jakarta.validation.constraints.NotNull;

Upvotes: 10

Guilherme Silva Sousa
Guilherme Silva Sousa

Reputation: 320

Jakarta Bean Validation API is the recommended dependency to import and solve this issue:

Maven

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

Gradle

implementation 'jakarta.validation:jakarta.validation-api:2.0.2'

Upvotes: 21

Lukasz Ochmanski
Lukasz Ochmanski

Reputation: 1210

For gradle users add this dependency to build.gradle file:

implementation 'javax.validation:validation-api:2.0.1.Final'

Upvotes: 10

Kimo_do
Kimo_do

Reputation: 1038

Validation Starter no longer included in web starters from the Spring Boot version 2.3 : https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-2.3-Release-Notes#validation-starter-no-longer-included-in-web-starters

you should add it yourself.

Upvotes: 42

sproketboy
sproketboy

Reputation: 9468

I used it like this in my Spring app and it worked.

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

Upvotes: 48

Related Questions