Java Bean验证:具有类级别约束的GroupSequence [英] Java Bean Validation: GroupSequence with Class-Level Constraint

查看:759
本文介绍了Java Bean验证:具有类级别约束的GroupSequence的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有多个(自定义)内部约束和一个类级约束的Bean类.我想先验证内部约束,再执行类级别约束.代码如下:

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只是一个位于某个位置的空接口).

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

但是,当我尝试运行此命令时,类级别的约束根本没有得到验证.当我尝试像这样定义GroupSequence时

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 })

(Outer是一个随机接口),我得到了例外:

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

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

s/o是否知道如何确保在内部约束之后对类级别约束进行验证? (这似乎不是默认设置!一段时间后,我突然出现了一些随机问题.)

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.)

推荐答案

尝试一下:

@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;
}

根据规范 NewSlotBean 只是默认组的替代者.另请参阅Bean验证规范的3.4.3节:

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:

由于序列不能具有循环依赖性,因此在 序列的声明不是一种选择.约束在 A类,并属于默认"组(默认为 明确地)隐含地属于组A.

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类上定义的序列(即重新定义默认组) 该类)必须包含组A.换句话说,默认 托管在类上的约束必须是序列定义的一部分. 如果@GroupSequence重新定义了A类的默认组 不包含组A,则在发生以下情况时会引发GroupDefinitionException 约束声明和验证过程的类已验证 或请求其元数据时.

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.

这篇关于Java Bean验证:具有类级别约束的GroupSequence的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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