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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4106772/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 11:41:22  来源:igfitidea点击:

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

javabean-validationhibernate-validatorjsr

提问by cam

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?

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

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

采纳答案by sourcedelica

Yes, just add @Validto the collection.

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

Here is an examplefrom the Hibernate Validator Reference.

这是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.

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

回答by Hardy

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:

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

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?

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

No

回答by dezzer10

You, can also add @NotEmptyto the collection.

您,也可以添加@NotEmpty到集合中。

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 @Validannotation ensures that each Personobject is validated

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

回答by holmis83

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 Hymanson library to deserialize JSON you can add @JsonCreatorannotation on the constructor and Hymanson will automatically deserialize JSON array to wrapper object.

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

回答by Hamid Mohayeji

Both of these approaches work:

这两种方法都有效:

class MyDto {

    private List<@Valid MyBean> beans;
}

or

或者

class MyDto {

    @Valid
    private List<MyBean> beans;
}