作者热门文章
- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
是否可以在 JSR 303 - Jave Bean Validation 中验证集合本身没有任何注释但其中包含的元素的对象集合?
例如,这是否可能由于第二个人的空名称而导致违反约束:
List<Person> people = new ArrayList<Person>();
people.add(new Person("dave"));
people.add(new Person(null));
Validator validator = Validation.buildDefaultValidatorFactory().getValidator();
Set<ConstraintViolation<List<Person>>> validation = validator.validate(people);
最佳答案
是的,只需将 @Valid
添加到集合中即可。
这里是 an example来自 Hibernate Validator Reference。
public class Car {
@NotNull
@Valid
private List<Person> passengers = new ArrayList<Person>();
}
这是标准的 JSR-303 行为。见 spec 的第 3.1.3 节.
关于java - JSR 303 : How to Validate a Collection of annotated objects?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4106772/
我是一名优秀的程序员,十分优秀!