【问题标题】:Validation Group inheritance验证组继承
【发布时间】:2014-10-25 14:44:51
【问题描述】:

给定以下类和接口

class Person {

  @NotNull(groups=Both.class)
  private String name;
}

验证组:

interface One {}
interface Two {}
interface Both extends One, Two {}

打电话时

Person person = new Person();

validator.validate(person, One.class) 

validator.validate(person, Two.class)  

我希望它应该是无效的,因为 name 是 null 但它不是。 事实证明,Both.class 组仅在用于 validator.validate(..., Both.class) 方法时才有用。

【问题讨论】:

  • 你有什么问题?
  • @hooknc 我希望它应该是无效的,因为 name 是 null 但它不是。

标签: java hibernate bean-validation hibernate-validator


【解决方案1】:

你继承了错误的方向。 Both 组扩展了 OneTwo 您可能会将其视为“Both 包含 OneTwo”。这意味着,每次您针对Both 组进行验证时,组OneTwo 的所有约束也包含在Both 中,因此得到验证。

但反之则不然:属于Both 组的约束不属于One,也不属于Two。因此,如果您验证组 One 的约束,则不会验证属于 Both 的约束。

参考http://beanvalidation.org/1.1/spec/#constraintdeclarationvalidationprocess-groupsequence-groupinheritance

【讨论】:

  • 谢谢!如果我有很多组(A、B、C、D、E、F.class),我必须在注释@NotNull 中将它们全部列出(groups = {A,B,C,D,E,F.class} )?不好。
猜你喜欢
  • 2013-01-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-17
  • 1970-01-01
  • 2023-02-15
  • 1970-01-01
相关资源
最近更新 更多