Java Bean Validation: GroupSequence with Class-Level Constraint

馋奶兔 提交于 2019-12-21 10:14:21

问题


I have a bean class with multiple (custom) inner constraints and one class-level constraint. I'd like to validate the inner constraints before the class-level constraint. The code looks like this:

@GroupSequence({ Inner.class, NewSlotBean.class })
@TotalBeanValid(groups = NewSlotBean.class)
public class NewSlotBean {

    @DayMonthYearString(groups = Inner.class)
    private String slotDay;

    @TimeString(groups = Inner.class)
    private String slotBegin;

    @LengthString(groups = Inner.class)
    private String slotLength;
}

(Inner is just an empty interface lying around somewhere).

However, when I try to run this, the class-level constraint does not get validated at all. When I try to define the GroupSequence like

@GroupSequence({ Inner.class, Outer.class })

(with Outer being a random interface), I get the exception:

javax.validation.GroupDefinitionException: ...*.beans.NewSlotBean must be part of the redefined default group sequence.

Does s/o know how to make sure that the class-level constraint is validated after the inner ones? (This appears not to be the default! I've had random problems with it popping up after a while.)


回答1:


Try this:

@GroupSequence({ Inner.class, NewSlotBean.class })
@TotalBeanValid(groups = Default.class)
public class NewSlotBean {

    @DayMonthYearString(groups = Inner.class)
    private String slotDay;

    @TimeString(groups = Inner.class)
    private String slotBegin;

    @LengthString(groups = Inner.class)
    private String slotLength;
}

According to the spec NewSlotBean is just a stand-in for the default group. See also section 3.4.3 of the Bean Validation spec:

Since sequences cannot have circular dependencies, using Default in the declaration of a sequence is not an option. Constraints hosted on a class A and belonging to the Default group (by default or explicitly) implicitly belong to the group A.

A sequence defined on a class A (i.e. redefining the Default groups for the class) must contain the group A. In other words, the default constraints hosted on a class must be part of the sequence definition. If a @GroupSequence redefining the Default group for a class A does not contain the group A, a GroupDefinitionException is raised when Constraint declaration and validation process the class is validated or when its metadata is requested.



来源:https://stackoverflow.com/questions/10380209/java-bean-validation-groupsequence-with-class-level-constraint

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!