如何在Bean验证1.0中构造ConstraintViolationException? [英] How do I construct a ConstraintViolationException in Bean Validation 1.0?

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

问题描述

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

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

我应该如何声明违规集,以便可以在异常构造函数中使用它?

解决方案

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

 引发新的ConstraintViolationException(新的HashSet< ConstraintViolation>(违规)); 

您可能有兴趣跟踪解决此问题的 BVAL-198 ./p>

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.

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

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