我如何构造一个ConstraintViolationException? [英] How do I construct a ConstraintViolationException?

查看:263
本文介绍了我如何构造一个ConstraintViolationException?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对javax.validation API感到困惑。我正在写一个简单的测试来理解它:

 示例sample = new Sample(); 
Set< ConstraintViolation< Sample>> violation = validator.validate(sample);
if(!violations.isEmpty()){
// Eclipse拒绝让我使用我的违反变量
抛出新的ConstraintViolationException(违规);
}

我应该如何声明这组违规,以便在我的例外中使用它构造函数?

解决方案

您可以像这样解决这个问题:

 throw new ConstraintViolationException(
new HashSet< ConstraintViolation<>>>(violations));

您可能有兴趣跟踪 BVAL-198 ,它解决了这个问题。

I am puzzled by the javax.validation API. I am writing a simple test to understand it :

Sample sample = new Sample();
Set<ConstraintViolation<Sample>> violations = validator.validate(sample);
if (!violations.isEmpty()) {
    // Eclipse refuses to let me use my violations variable
    throw new ConstraintViolationException(violations);
}

How should I declare the set of violations so I can use it in my exception constructor?

解决方案

You can work around this like so:

throw new ConstraintViolationException(
    new HashSet<ConstraintViolation<?>>(violations));

You may be interested in tracking BVAL-198 which addresses this issue.

这篇关于我如何构造一个ConstraintViolationException?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆