1

我正在使用javax.validation.Validator相关类进行基于注释的验证。

Configuration<?> configuration = Validation.byDefaultProvider().configure();   
ValidatorFactory factory = configuration.buildValidatorFactory();   
Validator validator = factory.getValidator(); 

Set<ConstraintViolation<ValidatableObject>> constraintViolations = validator.validate(o);   
for (ConstraintViolation<ValidatableObject> value : constraintViolations) {   
     List< Class< ? extends ConstraintValidator< ? extends Annotation,?>>> list = 
             value.getConstraintDescriptor().getConstraintValidatorClasses();  
}

我收到一个编译错误,说明:

类型不匹配:无法从 List< Class< 转换?将 ConstraintValidator< capture#4-of ?,?>>> 扩展到 List< Class< ? 扩展 ConstraintValidator< ? 扩展注释,?>>>

我错过了什么?

4

1 回答 1

0

如果您不介意使用ConstraintValidator<?, ?>(即您不需要ConstraintValidator<? extends Annotation, ?>),这应该可以:

Configuration<?> configuration = Validation.byDefaultProvider().configure();   
ValidatorFactory factory = configuration.buildValidatorFactory();   
Validator validator = factory.getValidator(); 

Set<ConstraintViolation<ValidatableObject>> constraintViolations = validator.validate(o);   
for (ConstraintViolation<ValidatableObject> value : constraintViolations) {   
     List< Class< ? extends ConstraintValidator<?,?>>> list = 
             value.getConstraintDescriptor().getConstraintValidatorClasses();  
}
于 2011-05-24T01:51:30.707 回答