我有一个bean对象,想要使用椭圆验证框架验证其字段。 e.g
public class Demo{
@Range(min=1)
private long id;
@NotNull
private long[] values;
}
如上所述,我有一个字段,其类型为long数组。我想确保数组不为null,并且其中的每个元素必须大于零。我不知道如何验证以确保其元素大于零。谁能告诉我?感谢。
答案 0 :(得分:1)
您可以使用applyTo属性:
@NotNull(appliesTo = {ConstraintTarget.CONTAINER, ConstraintTarget.VALUES})
private long[] values;
答案 1 :(得分: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;