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

后端 未结 5 1438
悲&欢浪女
悲&欢浪女 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:46

    You, can also add @NotEmpty to the collection.

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

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

提交回复
热议问题