简体   繁体   English

如何在 Bean Validation 1.0 中构造 ConstraintViolationException?

[英]How do I construct a ConstraintViolationException in Bean Validation 1.0?

I am puzzled by the javax.validation API.我对 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.您可能对跟踪解决此问题的BVAL-198感兴趣。

This is a known usability issue in Bean Validation 1.0.这是 Bean Validation 1.0 中的一个已知可用性问题。 This issue was addressed in Bean Validation 1.1 by issue BVAL-198 , "Simplify creation of ConstraintViolationExceptions".此问题已在Bean Validation 1.1 中BVAL-198问题“简化 ConstraintViolationExceptions 的创建”中得到解决。 Upgrading to Bean Validation 1.1 or later will allow your code to compile as written.升级到 Bean Validation 1.1 或更高版本将允许您的代码按照编写的方式进行编译。

The specific issue is that the ConstraintViolationException constructors accepted Set<ConstraintViolation<?>> for their constraintViolations parameter.具体问题是ConstraintViolationException构造函数接受Set<ConstraintViolation<?>>作为它们的constraintViolations参数。 Since Set<ConstraintViolation<Sample>> is not a subtype of Set<ConstraintViolation<?>> , it could not be passed into the constructor, with a compilation error occurring when attempting to do so.由于Set<ConstraintViolation<Sample>>不是Set<ConstraintViolation<?>>的子类型,因此无法将其传递到构造函数中,尝试这样做时会发生编译错误。

Bean validation 1.1.0 changed the constructors to instead accept Set<? extends ConstraintViolation<?>> Bean 验证 1.1.0 将构造函数更改为接受Set<? extends ConstraintViolation<?>> Set<? extends ConstraintViolation<?>> . Set<? extends ConstraintViolation<?>> As this is a supertype of Set<ConstraintViolation<Sample>> , it can be passed directly to the constructor.由于这是Set<ConstraintViolation<Sample>>的超类型,它可以直接传递给构造函数。

As mentioned in this other answer , the fix while still on Bean Validation 1.0 was to pass in a Set<ConstraintViolation<?>> instead of Set<ConstraintViolation<Sample>> :正如在另一个答案中提到的,仍然在 Bean Validation 1.0 上的修复是传入Set<ConstraintViolation<?>>而不是Set<ConstraintViolation<Sample>>

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何检查,抛出和捕获bean的验证ConstraintViolationException - How to check, throw and catch bean's validation ConstraintViolationException 如何自定义 Bean Validations ConstraintViolationException? - How to customize Bean Validations ConstraintViolationException? 如何解决 Spring Boot 中的 ConstraintViolationException - how do I solve the ConstraintViolationException in Spring Boot Bean验证引发ConstraintViolationException时自定义JAX-RS响应 - Customizing JAX-RS response when a ConstraintViolationException is thrown by Bean Validation 使用 webflux bean 验证时出现不明确的异常(WebExchangeBindException 与 ConstraintViolationException) - Ambiguous exception when using webflux bean validation (WebExchangeBindException vs ConstraintViolationException) 如何自动删除连接表中的行,以避免出现ConstraintViolationException? - How do I delete a row in a join table automatically, to avoid a ConstraintViolationException? 如何使用xml配置构造以下bean? - How do you construct the following bean with xml configuration? Java Bean验证:如何指定相同类型但具有不同组的多个验证约束? - Java Bean Validation: How do I specify multiple validation constraints of the same type but with different groups? 如何在列表字段中进行bean的条件验证 - How to do conditional validation of bean in a list field 为什么在抛出ConstraintViolationException(bean验证)时,Spring Data JPA给JPA实体一个id - Why is JPA entity given an id by Spring Data JPA when ConstraintViolationException thrown (bean validation)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM