This short article discusses how to compile projects data using javax.validation packages for Spring Boot 2.3.0 (and newer) applications.
The issue
Before Spring Boot 2.3.0, in order to compile javax.validation.* Classes, the only needed dependency was:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
As a matter of fact, this dependency wrapped (up to that release) also the spring-boot-starter-validation. This is visible from the ‘mvn dependency:tree’ command:
\- org.springframework.boot:spring-boot-starter-web:jar:2.2.6.RELEASE:compile +- org.springframework.boot:spring-boot-starter:jar:2.2.6.RELEASE:compile | .....altro..... +- org.springframework.boot:spring-boot-starter-json:jar:2.2.6.RELEASE:compile | .....altro..... +- org.springframework.boot:spring-boot-starter-tomcat:jar:2.2.6.RELEASE:compile | .....altro..... +- org.springframework.boot:spring-boot-starter-validation:jar:2.2.6.RELEASE:compile | +- jakarta.validation:jakarta.validation-api:jar:2.0.2:compile | \- org.hibernate.validator:hibernate-validator:jar:6.0.20.Final:compile
Since Spring Boot 2.3.0, you have to include explicitly the spring-boot-starter-validation dependency. The logic behind this choice is to reduce the size of Spring Boot Uber JAR files when validation is not mandatory for a Web application.
Here is the same ‘mvn dependency:tree’ command for a Spring Boot 2.6.1 application:
[INFO] +- org.springframework.boot:spring-boot-starter-web:jar:2.6.1:compile [INFO] | +- org.springframework.boot:spring-boot-starter:jar:2.6.1:compile [INFO] | | +- org.springframework.boot:spring-boot:jar:2.6.1:compile [INFO] | | +- org.springframework.boot:spring-boot-autoconfigure:jar:2.6.1:compile [INFO] | | +- org.springframework.boot:spring-boot-starter-logging:jar:2.6.1:compile [INFO] | | | +- ch.qos.logback:logback-classic:jar:1.2.7:compile [INFO] | | | | \- ch.qos.logback:logback-core:jar:1.2.7:compile [INFO] | | | +- org.apache.logging.log4j:log4j-to-slf4j:jar:2.14.1:compile [INFO] | | | | \- org.apache.logging.log4j:log4j-api:jar:2.14.1:compile [INFO] | | | \- org.slf4j:jul-to-slf4j:jar:1.7.32:compile [INFO] | | +- jakarta.annotation:jakarta.annotation-api:jar:1.3.5:compile [INFO] | | \- org.yaml:snakeyaml:jar:1.29:compile [INFO] | +- org.springframework.boot:spring-boot-starter-json:jar:2.6.1:compile [INFO] | | +- com.fasterxml.jackson.core:jackson-databind:jar:2.13.0:compile [INFO] | | | +- com.fasterxml.jackson.core:jackson-annotations:jar:2.13.0:compile [INFO] | | | \- com.fasterxml.jackson.core:jackson-core:jar:2.13.0:compile [INFO] | | +- com.fasterxml.jackson.datatype:jackson-datatype-jdk8:jar:2.13.0:compile [INFO] | | +- com.fasterxml.jackson.datatype:jackson-datatype-jsr310:jar:2.13.0:compile [INFO] | | \- com.fasterxml.jackson.module:jackson-module-parameter-names:jar:2.13.0:compile [INFO] | +- org.springframework.boot:spring-boot-starter-tomcat:jar:2.6.1:compile [INFO] | | +- org.apache.tomcat.embed:tomcat-embed-core:jar:9.0.55:compile [INFO] | | \- org.apache.tomcat.embed:tomcat-embed-websocket:jar:9.0.55:compile [INFO] | +- org.springframework:spring-web:jar:5.3.13:compile [INFO] | | \- org.springframework:spring-beans:jar:5.3.13:compile [INFO] | \- org.springframework:spring-webmvc:jar:5.3.13:compile [INFO] | +- org.springframework:spring-aop:jar:5.3.13:compile [INFO] | +- org.springframework:spring-context:jar:5.3.13:compile [INFO] | \- org.springframework:spring-expression:jar:5.3.13:compile
The spring-boot-starter-validation is not anymore in the spring-boot-starter-web, therefore if you try to compile older Java classes with a new Spring Boot parent class, the following errors will pop-up:
[ERROR] /home/francesco/git/masterspringboot/rest/demo-validation/src/main/java/com/example/testrest/CountryValidator.java:[3,24] package javax.validation does not exist [ERROR] /home/francesco/git/masterspringboot/rest/demo-validation/src/main/java/com/example/testrest/CountryValidator.java:[4,24] package javax.validation does not exist [ERROR] /home/francesco/git/masterspringboot/rest/demo-validation/src/main/java/com/example/testrest/CountryValidator.java:[8,42] cannot find symbol symbol: class ConstraintValidator
How to fix it
The solution is to add to your pom.xml the following dependency:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-validation</artifactId> </dependency>Found the article helpful? if so please follow us on Socials