package javax.validation.constraints does not exist
Below is a Spring Boot application and validation bean API example.
GlobalProperties.java
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotEmpty;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import org.springframework.validation.annotation.Validated;
@Component
@ConfigurationProperties
@Validated
public class GlobalProperties {
//@Value("${thread-pool}")
@Max(5)
@Min(0)
private int threadPool;
Run the Spring Boot application and hits the following error:
Terminal
package javax.validation.constraints does not exist
Solution – Spring Boot 2.3
Since Spring Boot 2.3, they no longer include the validation starter (bean validation APIs), so we must manually add it.
For Maven, we add the following dependencies:
pom.xml
<!-- Bean Validation API -->
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>2.0.1.Final</version>
</dependency>
<!-- Hibernate Validator (implementation of the Bean Validation) -->
<dependency>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator</artifactId>
<version>6.0.10.Final</version>
</dependency>
Solution – Spring Boot 3.x
Since Spring Boot 3, the Servelet APIs migrated from the package javax.*
to jakarta.*
, and we may hit similar errors:
Terminal
package jakarta.validation.constraints does not exist
For Maven, we add the following dependencies:
pom.xml
<!-- JSR 303 - Bean Validation API -->
<dependency>
<groupId>jakarta.validation</groupId>
<artifactId>jakarta.validation-api</artifactId>
<version>3.0.2</version>
</dependency>
<!-- Bean Validation API Reference Implementation -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>8.0.1.Final</version>
</dependency>
hi for me it did not solve the issue
java: package jakarta.validation.constraints does not exist
what is your version?
starter-parent version 3.2.5
error package javax.validation does not exist
Earlier it was working well with
org.springframework.boot
spring-boot-starter-web
2.7.5
I changed it to 3.2.5 so I’m getting error