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

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

    I wrote this generic class:

    public class ValidListWrapper {
    
        @Valid
        private List list;
    
        public ValidListWrapper(List list) {
            this.list = list;
        }
    
        public List 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.

提交回复
热议问题