java端使用@valid注解效验json数据格式中包含有对象和list集合

1.后端接收到的json的格式
{
“person”:{
“name”:“xiaoming”
},
“studentList”:[
{
“age”:“1”
},
{
“age”:“2”
}
]
}
2. 实体类

 @Data
 public class Person{
 	 @NotEmpty(message = "姓名不为空")
 	String name;
 }
 @Data
 public class Student{
   @NotNull(message = "年龄不为空")
    Integer age;
 }

3.控制器

 @RestController
 public class TestController {
   @PostMapping("/add")
   @ExceptionHandler
   public void add(@RequestBody @Valid TestAdd testAdd){
   }

5.新建一个TestAdd

 @Data
 public class TestAdd{
	@Valid
	Person person;
	@Valid
	ValidList<Student> studentList;
}
  1. 新建ValidList,效验list集合
public class ValidList<E> implements List<E> {
    @Valid
    private List<E> list;

   public ValidList() {
        this.list = new ArrayList<>();
    }

   public ValidList(List<E> list) {
        this.list = list;
    }

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

   public void setList(List<E> list) {
        this.list = list;
    }

   //重写List方法,可省略
   @Override
   public int size() {
        return list.size();
    }

   @Override
   public boolean isEmpty() {
        return list.isEmpty();
    }

   @Override
   public boolean contains(Object o) {
        return list.contains(o);
    }

    @Override
    public Iterator<E> iterator() {
        return list.iterator();
    }

    @Override
    public Object[] toArray() {
        return list.toArray();
    }

    @Override
    public <T> T[] toArray(T[] a) {
        return list.toArray(a);
    }

    @Override
    public boolean add(E e) {
        return list.add(e);
    }

    @Override
    public boolean remove(Object o) {
        return list.remove(o);
    }

    @Override
    public boolean containsAll(Collection<?> c) {
        return list.contains(c);
    }

    @Override
    public boolean addAll(Collection<? extends E> c) {
        return list.addAll(c);
    }

    @Override
    public boolean addAll(int index, Collection<? extends E> c) {
        return list.addAll(index, c);
    }

    @Override
    public boolean removeAll(Collection<?> c) {
        return list.removeAll(c);
    }

    @Override
    public boolean retainAll(Collection<?> c) {
        return list.retainAll(c);
    }

    @Override
    public void clear() {
        list.clear();
    }

    @Override
    public E get(int index) {
        return list.get(index);
    }

    @Override
    public E set(int index, E element) {
        return list.set(index, element);
    }

    @Override
    public void add(int index, E element) {
        list.add(index, element);
    }

    @Override
    public E remove(int index) {
        return list.remove(index);
    }

    @Override
    public int indexOf(Object o) {
        return list.indexOf(o);
    }

    @Override
    public int lastIndexOf(Object o) {
        return list.lastIndexOf(o);
    }

    @Override
    public ListIterator<E> listIterator() {
        return list.listIterator();
    }

    @Override
    public ListIterator<E> listIterator(int index) {
        return list.listIterator(index);
    }

    @Override
    public List<E> subList(int fromIndex, int toIndex) {
        return list.subList(fromIndex, toIndex);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值