@ NotNull.List的目的 [英] Purpose of @NotNull.List

查看:1854
本文介绍了@ NotNull.List的目的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在标准约束在Bean验证API(JSR-303)中,我找到了NotNull.List批注.它的描述是:

When I looked among the standard constraints in Bean Validation API (JSR-303), I found the NotNull.List annotation. Its description is:

在同一元素上定义几个@NotNull批注

Defines several @NotNull annotations on the same element

这是有效的语法:

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

但这没有任何意义.该对象为null或不是.您什么时候使用此注释?

But it makes no sense. Either the object is null or it is not. When would you use this annotation?

还有其他一些类似的注释,例如AssertFalse.ListAssertTrue.List.

There are several other similar annotations like AssertFalse.List and AssertTrue.List.

推荐答案

您可以具有多个@notNull注释,这些注释基于组属性是互斥的.

You can have multiple @NotNull annotations that are mutually exclusive based on the group attribute.

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

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

I do agree it's a little silly for this example since only the payload and message can be affected, but it's probably there to remain consistent with the other annotations.

请参见 查看全文

登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆