在类路径中找不到休眠验证器

hibernate validator not found in classpath

我写自定义注释

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>> = []
)

我为 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 在报告中解决 https://scans.gradle.com/s/ddjce3iwnldt4/dependencies?dependencies=valida&expandAll&focusedDependency=WzEsOCwxNDk2LFswLDEsWzE5XV1d

但是当我启动应用程序时我看到错误

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

完整日志https://scans.gradle.com/s/ddjce3iwnldt4/console-log#L101

为什么 spring 无法识别验证器?

Hibernate Validator 6 及更低版本实现了 Java EE Bean 验证 API。

Hibernate Validator 7 实现了(较新的)Jakarta Bean Validation API,它几乎相同,但 javax.validation 包重命名为 jakarta.validation

据我所知,Spring 框架仍在使用 Java EE Bean 验证。

所以你所做的是行不通的。消息正确,没有实现 Java EE Bean Validation;只有 Jakarta Bean Validation 的实现,Spring Framework 不支持它。

目前,如果您想使用 Spring Framework,您只能使用 Hibernate Validator 6。