简体   繁体   中英

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.

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>

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

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

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.

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'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM