JSR 303:如何验证带注释的对象的集合? [英] JSR 303: How to Validate a Collection of annotated objects?

查看:767
本文介绍了JSR 303:如何验证带注释的对象的集合?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能在JSR 303-Jave Bean验证中验证对象的集合,而该集合本身没有任何注释,但包含在其中的元素呢?

Is it possible to validate a collection of objects in JSR 303 - Jave Bean Validation where the collection itself does not have any annotations but the elements contained within do?

例如,由于第二个人的名字为空,是否有可能导致约束冲突:

For example, is it possible for this to result in a constraint violation due to a null name on the second person:

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添加到集合中即可.

Yes, just add @Valid to the collection.

这里是一个示例 Hibernate验证程序参考.

Here is an example from the Hibernate Validator Reference.

public class Car {
  @NotNull
  @Valid
  private List<Person> passengers = new ArrayList<Person>();
}

这是标准的JSR-303行为.请参见规范的第3.1.3节.

This is standard JSR-303 behavior. See Section 3.1.3 of the spec.

这篇关于JSR 303:如何验证带注释的对象的集合?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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