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

后端 未结 5 1339
悲&欢浪女
悲&欢浪女 2020-12-14 14:17

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 wit

5条回答
  •  醉梦人生
    2020-12-14 14:43

    Yes, just add @Valid to the collection.

    Here is an example from the Hibernate Validator Reference.

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

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

提交回复
热议问题