我有以下代码:
UniqueConstraintViolation violation = new UniqueConstraintViolationImpl();
Set<? extends javax.validation.ConstraintViolation> violations = new HashSet<UniqueConstraintViolation>();
violations.add(violation);
throw new javax.validation.ConstraintViolationException(violations);
其中UniqueConstraintViolation是接口扩展的javax.validation.ConstraintViolation
和UniqueConstraintViolationImpl是类实现的UniqueConstraintViolation
编译时出现以下错误
java:42:错误:找不到适合的方法
添加(UniqueConstraintViolation)
违反。添加(违反);
^
方法Collection.add(CAP#1)不适用
(参数不匹配; UniqueConstraintViolation无法转换为CAP#1)
方法Set.add(CAP#1)不适用
(参数不匹配;不能将UniqueConstraintViolation转换为CAP#1),其中CAP#1是新的类型变量:
CAP#1从捕获?扩展了ConstraintViolation。扩展ConstraintViolation
C:\ workspace \ zcts-travel \ crud-rest \ src \ main \ java \ ru \ zcts \ crud \ AbstractResource.java:43:错误:类型不兼容:Set无法转换为Set>
抛出新的javax.validation.ConstraintViolationException(violations);
^ where CAP#1 is a fresh type-variable:
CAP#1 extends ConstraintViolation from capture of ? extends ConstraintViolation
我只想通过我自己的javax.validation.ConstraintViolation实现将javax.validation.ConstraintViolationException构造函数传递给Set。
我该怎么办?
最佳答案
因此,有您的约束:
interface UniqueConstraintViolation extends ConstraintViolation<YourClass> {
}
public class UniqueConstraintViolationImpl implements UniqueConstraintViolation {
/* your impl here */
}
这将编译:
UniqueConstraintViolation violation = new UniqueConstraintViolationImpl();
Set<UniqueConstraintViolation> violations = new HashSet<UniqueConstraintViolation>();
violations.add(violation);
throw new ConstraintViolationException(violations);
关于java - Set中的GenericsAdd(捕获<?extended ConstraintViolation>)不能应用于(ConstraintViolation),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33668234/