|
对于最新稳定版本,请使用Spring Boot 4.0.0! |
验证
Bean Validation 1.1 支持的方法验证功能,只要 JSR-303 实现(如 Hibernate 验证器)在类路径上,就会自动启用。
这使得豆子方法可以注释为雅加达.验证对参数和/或返回值的约束。
带有此类注释方法的目标类需要用@Validated在类型层级为其方法进行内联约束注释。
例如,以下服务触发第一个参数的验证,确保其大小介于 8 和 10 之间:
-
Java
-
Kotlin
import jakarta.validation.constraints.Size;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
@Service
@Validated
public class MyBean {
public Archive findByCodeAndAuthor(@Size(min = 8, max = 10) String code, Author author) {
return ...
}
}
import jakarta.validation.constraints.Size
import org.springframework.stereotype.Service
import org.springframework.validation.annotation.Validated
@Service
@Validated
class MyBean {
fun findByCodeAndAuthor(code: @Size(min = 8, max = 10) String?, author: Author?): Archive? {
return null
}
}
应用的消息源用于解析{参数}在约束信息中。
这允许你使用你的申请messages.properties文件用于Beans验证消息。
参数解析完成后,消息插值通过Bean Validation的默认插值器完成。
以定制配置用于建造验证者工厂,定义一个ValidationConfigurationCustomizer豆。
当定义多个定制豆时,会根据它们的顺序命名@Order注释或命令实现。