版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u010588262/article/details/82776088
写了个接口:
@GetMapping("/do")
public int doSth(@RequestBody @Validated(MyGroup.class) List
发现@validated完全没用,但是用@valid可以,但是我要用分组功能必须用@validated,在stackOverflow上找到了解决方案:
https://stackoverflow.com/questions/28150405/validation-of-a-list-of-objects-in-spring
在项目里添加一个ValidList类即可,此类通用,可以在全部由此需求的项目(jdk1.8)中添加,无需改动,有ValidList类之后只要将接口方法参数中的List改成ValidList即可:
@GetMapping("/do")
public int doSth(@RequestBody @Validated(MyGroup.class) ValidList
注意:
如果你不用lombok,就把@Data注解去掉,给类中的list成员加上getter,setter方法即可
ValidList:
import lombok.Data;
import javax.validation.Valid;
import java.util.*;
@Data
public class ValidList
@Valid
private 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
return list.iterator();
}
@Override
public Object[] toArray() {
return list.toArray();
}
@Override
public
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.containsAll(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
return list.listIterator();
}
@Override
public ListIterator
return list.listIterator(index);
}
@Override
public List
return list.subList(fromIndex, toIndex);
}
}