gpt4 book ai didi

java - JSR 303 : How to Validate a Collection of annotated objects?

转载 作者:IT老高 更新时间:2023-10-28 20:39:53 25 4
gpt4 key购买 nike

是否可以在 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com