有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java JSR303验证:两个(或更多)验证约束:在第一次返回错误后,不计算第二次?

考虑到以下两个约束条件

@NotNull
@Pattern (pattern="foobar")
private String myFooBarMember;

如果第一个匹配(=返回错误)(@NotNull),是否有可能不计算第二个(@Pattern)?还是JSR303总是评估所有约束?如果有可能在第一场比赛后不评估第二场比赛,请说明如何进行评估

非常感谢


共 (1) 个答案

  1. # 1 楼答案

    我不是JSR303专家,但我从1.0 FR中找到了以下摘录:

    2.3. Constraint composition

    Note:

    If a composing constraint fails and if the composed constraint is marked as @ReportAsSingleViolation, the Bean Validation provider is free to not process the other composing constraints for this composed constraint.

    以及:

    3.5. Validation routine

    • for all reachable fields, execute all field level validations (including the ones expressed on superclasses) matching the targeted group unless the given validation constraint has already been processed during this validation routine for a given navigation path (see Section 3.5.1) as part of a previous group match.

    从后者来看,我认为默认情况下,所有验证器都将被执行

    从前者来看,我认为这(约束合成)将是实现你想要的一种方式。尽管它可能(也可能)依赖于Bean验证实现程序,所以您需要阅读一些专有文档