Spring Boot 3.0 package javax.validation does not exist
Asked Answered
P

1

18

I'm trying to use a method parameter validation feature supported by Bean Validation 1.1. For instance, the following method triggers the validation of the first parameter, making sure it's valid:

public String generateOtp(@Valid TotpAuthenticatorForm form, BindingResult bindingResult)

When I build a Spring Boot 2.7.7 project it's fine, but building a Spring Boot 3.0.1 project fails with a compilation error:

package javax.validation does not exist

How do I fix the issue?

Persas answered 11/1, 2023 at 15:29 Comment(0)
P
23

According to the release-notes, Spring Boot 3.0 has migrated from Java EE to Jakarta EE APIs for all dependencies, including:

Jakarta Validation 3.0
  • Spring Boot 2.7 - Jakarta Bean Validation 2.0
  • Spring Boot 3.0 - Jakarta Bean Validation 3.0

You can fix the issue by using Jakarta Bean Validation 3.0. Simply update import statements:

javax.validation
->
jakarta.validation
Persas answered 11/1, 2023 at 15:29 Comment(7)
It's work mentioning you need to include the following dependency, if you haven't yet: <dependency><groupId>org.hibernate</groupId><artifactId>hibernate-validator</artifactId><version>8.0.0.Final</version></dependency>Jasminejason
@dbaltor, we don't need to include it. Instead, we should use the spring-boot-starter-validation starter for using Java Bean Validation with Hibernate Validator in a Spring Boot project. See the pom.xml for the example.Persas
You're right @Boris. I knew about that with SB 2.x but that wasn't working with my current SB 3 project. I rebuilt everything with mvn clean package from the command line and it worked. I guess Intellij tripped down on this one. Thanks!Jasminejason
I tried all the solution you proposed but nothing seems to work in my case. I am using jakarta packages and I am keeping doing mvn clean package and mvn clean install and rerun my app in intellij but validation is simply not working at all.Kidder
@FrancescoDassisis let's discuss it here.Persas
After adding the annotation @NotBlank in my REST API, using the library spring-boot-starter-validation version 3.1.0 my project stopped working giving a conflict between the Java version we use in our environment (JDK 8) and the version that the hibernate-validation library (inherited) is built (JDK 11).Immobility
@FelipeCaparelli, please read the release notes linked in my answer - "Spring Boot 3.0 requires Java 17 as a minimum version. If you are currently using Java 8 or Java 11, you’ll need to upgrade your JDK before you can develop Spring Boot 3.0 applications."Persas

© 2022 - 2024 — McMap. All rights reserved.