此版本仍在开发中,尚未被视为稳定版本。对于最新的稳定版本,请使用 Spring Boot 3.4.3! |
验证
只要 JSR-303 实现(例如 Hibernate 验证器)在 Classpath 上,Bean Validation 1.1 支持的方法验证功能就会自动启用。
这允许 bean 方法使用jakarta.validation
对其参数和/或返回值的约束。
具有此类 Comments 方法的目标类需要使用@Validated
annotation 的 Comments,以便搜索其方法的内联约束 Comments。
例如,以下服务触发第一个参数的验证,确保其大小介于 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
}
}
The application’s MessageSource
is used when resolving {parameters}
in constraint messages.
This allows you to use your application’s messages.properties
files for Bean Validation messages.
Once the parameters have been resolved, message interpolation is completed using Bean Validation’s default interpolator.
To customize the Configuration
used to build the ValidatorFactory
, define a ValidationConfigurationCustomizer
bean.
When multiple customizer beans are defined, they are called in order based on their @Order
annotation or Ordered
implementation.