我在我的 gradle 构建中定义了以下依赖项(Spring Boot 版本是 2.7.7)
// Spring Boot
implementation group: "org.springframework.boot", name: "spring-boot-starter-actuator"
implementation group: "org.springframework.boot", name: "spring-boot-starter-validation"
implementation group: 'org.springframework.boot', name: 'spring-boot-autoconfigure'
implementation group: "org.springframework.boot", name: "spring-boot-starter-web"
implementation group: "org.springframework.boot", name: "spring-boot-starter-mail"
// Swagger/OpenAPI CodeGen
implementation group: 'org.springdoc', name: 'springdoc-openapi-ui', version: '1.6.14'
implementation group: 'org.springdoc', name: 'springdoc-openapi-common', version: '1.6.14'
implementation group: "org.openapitools", name: "jackson-databind-nullable", version: "0.2.4"
compileOnly group: "org.projectlombok", name: "lombok"
annotationProcessor group: "org.springframework.boot", name: "spring-boot-configuration-processor"
// Utils
implementation 'commons-io:commons-io:2.8.0'`
在我的服务中,我有以下实现
@Override
public List<MyObjectValue> getApplicationDetailFromSearch(@Valid MySearchModel mySearchModel) {
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
Validator validator = factory.getValidator();
Set<ConstraintViolation<MySearchModel>> violations = validator.validate(mySearchModel);
if (!violations.isEmpty()) {
throw new ConstraintViolationException(violations);
}
MySearchModel
是一条记录 带有 @MySearch
注释和相应的 MySearchValidator
实现
结果,当我调用验证器时是
javax.validation.NoProviderFoundException: Unable to create a Configuration, because no Jakarta Bean Validation provider could be found. Add a provider like Hibernate Validator (RI) to your classpath.
at javax.validation.Validation$GenericBootstrapImpl.configure(Validation.java:291) ~[jakarta.validation-api-2.0.2.jar:na]
at javax.validation.Validation.buildDefaultValidatorFactory(Validation.java:103) ~[jakarta.validation-api-2.0.2.jar:na]
at