JSR 303:如何验证带注释的对象集合?

发布于 2024-10-01 03:55:51 字数 436 浏览 5 评论 0原文

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

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);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

氛圍 2024-10-08 03:55:51

是的,只需将 @Valid 添加到集合中即可。

这是 Hibernate 验证器的示例参考。

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

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

Yes, just add @Valid to the collection.

Here is an example from the Hibernate Validator Reference.

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

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

轮廓§ 2024-10-08 03:55:51

您还可以将 @NotEmpty 添加到集合中。

public class Car {
  @NotEmpty(message="At least one passenger is required")
  @Valid
  private List<Person> passengers = new ArrayList<Person>();
}

这将确保至少有一名乘客在场,并且 @Valid 注释可确保每个 Person 对象都经过验证

You, can also add @NotEmpty to the collection.

public class Car {
  @NotEmpty(message="At least one passenger is required")
  @Valid
  private List<Person> passengers = new ArrayList<Person>();
}

this will ensure at least one passenger is present, and the @Valid annotation ensures that each Person object is validated

一张白纸 2024-10-08 03:55:51

从 Bean Validator 2.0 开始,这两种方法都有效

class MyDto {

    private List<@Valid MyBean> beans;
}

class MyDto {

    @Valid
    private List<MyBean> beans;
}

As of Bean Validator 2.0, both of these approaches work:

class MyDto {

    private List<@Valid MyBean> beans;
}

and

class MyDto {

    @Valid
    private List<MyBean> beans;
}
怪我鬧 2024-10-08 03:55:51

当然,您也可以迭代列表并对每个元素调用 Validator.validate。或者将 List 放入某个包装器 bean 中并使用 @Valid 对其进行注释。扩展 ArrayList 进行验证对我来说似乎是错误的。
您有想要用此解决的特定用例吗?如果是这样,也许你可以多解释一下。回答你最初的问题:

是否可以验证
JSR 303 中的对象集合 -
Jave Bean 验证
集合本身没有任何
注释但包含的元素
内做什么?

You can of course also just iterate over the list and call Validator.validate on each element. Or put the List into some wrapper bean and annotate it with @Valid. Extending ArrayList for validation seems wrong to me.
Do you have a particular use case you want to solve with this? If so maybe you can explain it a little more. To answer your initial question:

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?

No

去了角落 2024-10-08 03:55:51

我写了这个通用类:

public class ValidListWrapper<T> {

    @Valid
    private List<T> list;

    public ValidListWrapper(List<T> list) {
        this.list = list;
    }

    public List<T> getList() {
        return list;
    }

}

如果您使用 Jackson 库来反序列化 JSON,您可以在构造函数上添加 @JsonCreator 注释,Jackson 会自动将 JSON 数组反序列化为包装对象。

I wrote this generic class:

public class ValidListWrapper<T> {

    @Valid
    private List<T> list;

    public ValidListWrapper(List<T> list) {
        this.list = list;
    }

    public List<T> getList() {
        return list;
    }

}

If you are using Jackson library to deserialize JSON you can add @JsonCreator annotation on the constructor and Jackson will automatically deserialize JSON array to wrapper object.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文