10

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?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Boris
  • 22,667
  • 16
  • 50
  • 71

1 Answers1

10

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
Boris
  • 22,667
  • 16
  • 50
  • 71
  • It's work mentioning you need to include the following dependency, if you haven't yet: `org.hibernatehibernate-validator8.0.0.Final` – dbaltor Apr 04 '23 at 14:30
  • @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](https://bitbucket.org/borisredkin/totp-authenticator/src/master/pom.xml#lines-46:49) for the example. – Boris Apr 04 '23 at 16:11
  • 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! – dbaltor Apr 05 '23 at 11:57
  • 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. – Francesco Dassisis Jul 07 '23 at 14:16
  • @FrancescoDassisis let's discuss it [here](https://chat.stackoverflow.com/rooms/254399/room-for-boris-and-francesco-dassisis). – Boris Jul 07 '23 at 18:27
  • 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). – FelipeCaparelli Aug 08 '23 at 07:39
  • @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." – Boris Aug 08 '23 at 20:46