How do I construct a ConstraintViolationException in Bean Validation 1.0?

前端 未结 2 617
遇见更好的自我
遇见更好的自我 2021-02-12 19:26

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

Sample sample = new Sample();
Set>          


        
2条回答
  •  隐瞒了意图╮
    2021-02-12 20:00

    This is a known usability issue in Bean Validation 1.0. This issue was addressed in Bean Validation 1.1 by issue BVAL-198, "Simplify creation of ConstraintViolationExceptions". Upgrading to Bean Validation 1.1 or later will allow your code to compile as written.

    The specific issue is that the ConstraintViolationException constructors accepted Set> for their constraintViolations parameter. Since Set> is not a subtype of Set>, it could not be passed into the constructor, with a compilation error occurring when attempting to do so.

    Bean validation 1.1.0 changed the constructors to instead accept Set>. As this is a supertype of Set>, it can be passed directly to the constructor.

    As mentioned in this other answer, the fix while still on Bean Validation 1.0 was to pass in a Set> instead of Set>:

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

提交回复
热议问题