Main Tutorials

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>

References

About Author

author image
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

Subscribe
Notify of
1 Comment
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
Andreea
3 minutes ago

hi for me it did not solve the issue
java: package jakarta.validation.constraints does not exist