@NotNull.List 的用途

Purpose of @NotNull.List

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

Defines several @NotNull annotations on the same element

这是有效的语法:

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

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

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

您可以有多个@NotNull 注释,它们基于组属性相互排斥。

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

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

有关详细信息,请参阅 here

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

In the bean validation API javadoc, for every constraint annotation, there's a corresponding .List annotation. For example, for @NotNull, there's @NotNull.List, for which JavaDoc says:

Defines several @NotNull annotations on the same element

What would you accomplish with multiple @NotNull annotations that you cannot accomplish with one @NotNull?