gpt4 book ai didi

Java Bean 验证 : GroupSequence with Class-Level Constraint

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:16:12 24 4
gpt4 key购买 nike

我有一个具有多个(自定义)内部约束和一个类级约束的 bean 类。我想在类级约束之前验证内部约束。代码如下所示:

@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 只是一个位于某处的空接口(interface))。

但是,当我尝试运行它时,类级约束根本没有得到验证。当我尝试像这样定义 GroupSequence 时

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

(Outer 是一个随机接口(interface)),我得到异常:

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

s/o 是否知道如何确保类级约束在内部约束之后得到验证? (这似乎不是默认设置!我遇到过一段时间后弹出的随机问题。)

最佳答案

试试这个:

@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 节:

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.

关于Java Bean 验证 : GroupSequence with Class-Level Constraint,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10380209/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com