Spring参数校验--List类型参数校验

1.遇到的问题:

今天开发接口,遇到请求参数固定为List类型的,如下。发现参数校验失效。


    // 控制器
    @RequestMapping(value = "/saveVideo", method = RequestMethod.POST)
    @ResponseBody
    public List saveVideo(@Validated @RequestBody List req, BindingResult bindingResult) {
        // 省略业务代码
    }

    // VO
public class SaveVideoReq implements Vo {

    private static final long serialVersionUID = 1L;

    @NotBlank(message = "系统编号不能为空")
    private String sysNo;

    @NotBlank(message = "影像类型不能为空")
    private String videoType;

    @NotBlank(message = "影像名称不能为空")
    private String videoName;
}

2.解决方案一:

新建一个类,把List作为成员变量, 然后用@Valid注解在List上。

public class SaveVideoReqWrapper implements Vo {

    @Valid
    private List req;

}

3. 解决方案二:

但是,由于某种原因,前端只会传[{“sysNo”:“HIKID098”,“videoType”:“01”,“videoName":"高清小视频"},{...}]。因此,请求对象是固定的,不能用方案一处理。此时,可以参考网上的一个方法,创建一个类,实现List接口,并添加List list = new ArrayList<>();或者LinkedList作为成员变量,如下:

/**
 * FileName: ValidList
 * DESCRIPTION: 参数校验工具类, 用于校验List 类型的请求参数
 * @author: SLY
 * @create: 2019/3/5
 */
public class ValidList implements List {

    @Valid
    private List list = new ArrayList<>();

    @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 iterator() {
        return list.iterator();
    }

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

    @Override
    public  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.containsAll(c);
    }

    @Override
    public boolean addAll(Collection c) {
        return list.addAll(c);
    }

    @Override
    public boolean addAll(int index, Collection 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 listIterator() {
        return list.listIterator();
    }

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

    @Override
    public List subList(int fromIndex, int toIndex) {
        return list.subList(fromIndex, toIndex);
    }

    public List getList() {
        return list;
    }

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

然后,控制器的参数列表就变成了如下。此时,就可以正常从bingdingResult获取校验结果了。


    // 控制器
    @RequestMapping(value = "/saveVideo", method = RequestMethod.POST)
    @ResponseBody
    public List saveVideo(@Validated @RequestBody ValidList req, BindingResult bindingResult) {
        // 省略业务代码
    }

 

你可能感兴趣的:(Spring临床经验)