为什么我的编译器不能转换它?
错误:
Type mismatch: cannot convert from Set<ConstraintViolation<capture#1-of ?>> to Set<ConstraintViolation<Class<?>>>
我的代码:
public class validateValue(Class<?> beanType, String propertyName, Object value, Class<?>... groups){
TraversableResolver tr = new MyTraversableResolver();
Validator validator = Validation.buildDefaultValidatorFactory().usingContext().traversableResolver(tr).getValidator();
final Set<ConstraintViolation<Class<?>>> constraintViolations = validator.validateValue(beanType, propertyName, value, groups);
}
private class MyTraversableResolver implements TraversableResolver {
public boolean isReachable(Object traversableObject, Path.Node traversableProperty, Class<?> rootBeanType, Path pathToTraversableObject, ElementType elementType) {
return traversableObject == null || Hibernate.isInitialized(traversableObject);
}
public boolean isCascadable(Object traversableObject, Path.Node traversableProperty, Class<?> rootBeanType, Path pathToTraversableObject, ElementType elementType) {
return true;
}
}
Error:
Type mismatch: cannot convert from Set<ConstraintViolation<capture#1-of ?>> to Set<ConstraintViolation<Class<?>>>
My code:
public class validateValue(Class<?> beanType, String propertyName, Object value, Class<?>... groups){
TraversableResolver tr = new MyTraversableResolver();
Validator validator = Validation.buildDefaultValidatorFactory().usingContext().traversableResolver(tr).getValidator();
final Set<ConstraintViolation<Class<?>>> constraintViolations = validator.validateValue(beanType, propertyName, value, groups);
}
private class MyTraversableResolver implements TraversableResolver {
public boolean isReachable(Object traversableObject, Path.Node traversableProperty, Class<?> rootBeanType, Path pathToTraversableObject, ElementType elementType) {
return traversableObject == null || Hibernate.isInitialized(traversableObject);
}
public boolean isCascadable(Object traversableObject, Path.Node traversableProperty, Class<?> rootBeanType, Path pathToTraversableObject, ElementType elementType) {
return true;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为问题是你的编译器不知道你的 beanType 变量的类型与集合匹配。尝试这样的事情(代码可能需要稍微调整一下)
:
然后更改
I think the problem is your compiler doesn't know that the type of your beanType variable matches the set. Try something like this (code may need to be tweaked a bit)
Then changing:
to
@JustinKSU 将通配符 (?) 更改为类型 arg (T) 是正确的。另一个问题是
validator.validateValue(ClassbeanType, String propertyName, Object value, Class... groups)
不会返回一组对该类型的约束违规类,这将是:...而是返回类型上的一组约束违规:
这是一个细微的差别,但如果您仔细检查错误消息并计算尖括号,就会很明显。
所以,你的代码应该如下所示。为了示例的目的,我简化了验证器的创建。我还添加了一个返回值,因为我假设您想对结果做一些事情。
@JustinKSU is correct about changing the wildcard (?) to a type arg (T). The other issue is that
validator.validateValue(Class<T> beanType, String propertyName, Object value, Class<?>... groups)
doesn't return a set of constraint violations on the type's class, which would be:...but rather it returns a set of constraint violations on the type:
It's a subtle difference to see, but it's noticeable if you check the error message closely and count the angle brackets.
So, your code should be as below. I simplified the validator creation for purposes of the example. I also added a return value since I assume you want to do something with the results.