spring-boothibernate-validatorspring-validator

hibernate validator not found in classpath


I write custom annotation

package ru.tinkoff.bpm.verificationcheckservice.support.validation

import org.hibernate.validator.constraints.CompositionType
import org.hibernate.validator.constraints.ConstraintComposition
import org.hibernate.validator.constraints.URL
import javax.validation.Constraint
import javax.validation.Payload
import javax.validation.ReportAsSingleViolation
import javax.validation.constraints.NotBlank
import kotlin.annotation.AnnotationRetention.RUNTIME
import kotlin.annotation.AnnotationTarget.FIELD
import kotlin.reflect.KClass

@ConstraintComposition(CompositionType.AND)
@URL
@NotBlank
@ReportAsSingleViolation
@Target(FIELD)
@Retention(RUNTIME)
@Constraint(validatedBy = [])
annotation class NotBlankUrl(
    val message: String = "must be not blank and valid URL",
    val groups: Array<KClass<*>> = [],
    val payload: Array<KClass<out Payload>> = []
)

I add dependency for build.gradle.kts

implementation("org.springframework.boot:spring-boot-starter-data-jpa")
implementation("org.springframework.boot:spring-boot-starter-validation")
implementation("org.hibernate:hibernate-validator:7.0.1.Final")

Gradle resolve it in report https://scans.gradle.com/s/ddjce3iwnldt4/dependencies?dependencies=valida&expandAll&focusedDependency=WzEsOCwxNDk2LFswLDEsWzE5XV1d

But when i start application i see error

The Bean Validation API is on the classpath but no implementation could be found

Action:

Add an implementation, such as Hibernate Validator, to the classpath

full logs https://scans.gradle.com/s/ddjce3iwnldt4/console-log#L101

Why spring does not recognize validator?


Solution

  • Hibernate Validator 6 and below implements the Java EE Bean Validation API.

    Hibernate Validator 7 implements the (newer) Jakarta Bean Validation API, which is almost the same but with the javax.validation package renamed to jakarta.validation.

    As far as I know, Spring Framework is still using Java EE Bean Validation.

    So what you are doing will not work. The message is correct, there is no implementation of Java EE Bean Validation in your classpath; only an implementation of Jakarta Bean Validation, and Spring Framework doesn't support that.

    For now, if you want to use Spring Framework, you're stuck with Hibernate Validator 6.