2

我有一个 bean 对象,想使用椭圆验证框架来验证它的字段。例如

public class Demo{

    @Range(min=1)
    private long id;

    @NotNull
    private long[] values;
}

如上所述,我有一个类型为 long 数组的字段。我想确保数组不为空,并且其中的每个元素都必须大于零。我不知道如何验证以确保其元素大于零。谁能告诉我?谢谢。

4

2 回答 2

4

您可以使用 applyTo 属性:

@NotNull(appliesTo = {ConstraintTarget.CONTAINER, ConstraintTarget.VALUES})
private long[] values;
于 2012-08-03T00:19:21.960 回答
1
/* ConstraintTarget.CONTAINER(i.e List) and 
 * ConstraintTarget.VALUES( i.e List elements)
 * it means List shouldn't be null & Its elements shouldn't be null 
 */
@NotNull(appliesTo= {ConstraintTarget.CONTAINER,ConstraintTarget.VALUES})
/*
 * List size should be greater than or equal to 1
 */
@Size(min=1)
/*
 * ConstraintTarget.VALUES means List elements shouldn't be empty
 */
@NotEmpty(appliesTo = {ConstraintTarget.VALUES})
private List<String> roles;
于 2019-08-07T04:00:37.783 回答