【问题标题】:Purpose of @NotNull.List@NotNull.List 的目的
【发布时间】:2015-04-21 01:23:47
【问题描述】:

当我查看 Bean Validation API (JSR-303) 中的标准 constraints 时,我发现了 NotNull.List 注释。它的描述是:

在同一个元素上定义多个@NotNull 注解

这是有效的语法:

@NotNull.List({@NotNull, @NotNull})
private Object myObject;

但这没有任何意义。该对象要么为空,要么不为空。你什么时候使用这个注解?

还有其他几个类似的注解,如AssertFalse.ListAssertTrue.List

【问题讨论】:

    标签: java bean-validation hibernate-validator


    【解决方案1】:

    对于@NotNull 的情况,可能需要多个@NotNull 注释 对于@dfb 解释的不同验证组。但同样可能 通过在 groups 属性中列出这些组来完成。 这个很好解释here with test cases

    在 bean 验证 API javadoc 中,对于每个约束注解, 有一个相应的 .List 注释。例如,对于@NotNull, 有@NotNull.List,JavaDoc 对此表示:

    在同一个元素上定义多个@NotNull注解

    你会用多个 @NotNull 注释来完成什么? 不能用一个@NotNull 完成?

    【讨论】:

      【解决方案2】:

      你可以有多个基于组属性互斥的@NotNull注解。

      @NotNull.List({@NotNull(groups=Foo.class,message="Some message!"), 
                     @NotNull(groups=bar.class, message="Some other message!"})
      private Object myObject;
      

      我同意这个例子有点傻,因为只有有效负载和消息会受到影响,但它可能与其他注释保持一致。

      更多详情请见here

      【讨论】:

        猜你喜欢
        • 2016-06-03
        • 1970-01-01
        • 1970-01-01
        • 2015-10-06
        • 2014-01-30
        • 2011-09-26
        • 2019-12-17
        • 2017-12-26
        • 2012-05-17
        相关资源
        最近更新 更多