@Valid验证List⾥⾯参数
我⼀开始是这么写的,打断点,⼀直显⽰errors为0,然后百度,发现@Vaild⽆法验证递归验证List。
public ResResult saveGuardianeApp(@Valid @RequestBody List<GuardianAddReq> guardianAddReqs,
BindingResult br) {}
后来经过⼀番查询测试终于搞定:
public ResResult saveGuardianeApp(@Valid @RequestBody ValidableList<GuardianAddReq> guardianAddReqs, BindingResult br) {}
下⾯是List⼦类,可以完美解决上述问题
import lombok.Data;
import javax.validation.Valid;
import java.util.*;
/**
*
* @param <E>
* Author zkn
*/
@Data
public class ValidableList<E> implements List<E> {
@Valid
private List<E> list = new LinkedList<>();
@Override
public int size() {
return list.size();
}
@Override
public boolean isEmpty() {
return list.isEmpty();
}
@Override
public boolean contains(Object o) {
ains(o);
}
@Override
public Iterator<E> iterator() {
return list.iterator();
}
@Override
public Object[] toArray() {
Array();
}
@Override
public <T> T[] toArray(T[] a) {
public boolean add(E e) {
return list.add(e);
}
@Override
public boolean remove(Object o) {
ve(o);
}
@Override
public boolean containsAll(Collection<?> c) {
ainsAll(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) {
veAll(c);
}
@Override
public boolean retainAll(Collection<?> c) {
ainAll(c);
}
@Override
public void clear() {
list.clear();
}
@Override
public E get(int index) {
(index);
}
@Override
public E t(int index, E element) {
return list.t(index, element);
}
@Override
public void add(int index, E element) {
list.add(index, element);
}
@Override
public E remove(int index) {
ve(index);
}
@Override
public int indexOf(Object o) {
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);
}
}