约束验证

时间:2010-04-26 03:54:41

标签: java generics constraints bean-validation

我正在使用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&lt;转换类&LT; ?扩展ConstraintValidator&lt;捕获#4-of?,?&gt;&gt;&gt;列出&lt;类&LT; ?扩展ConstraintValidator&lt; ?扩展注释,?&gt;&gt;&gt;

我错过了什么?

1 个答案:

答案 0 :(得分: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();  
}