SpringBoot关于List集合的校验

1. List集合失效的校验

    @PostMapping("saveBatch")
    @ApiOperation("批量保存")
    @LogOperation("批量保存")
    public Result saveBatch(@RequestBody @Validated List list) {
        resultFieldService.saveBatch(list);
        return Result.ok();
    }

2. 针对List集合的校验有两种方案

2.1 第一种方案

重新定义一个集合ValidList,需继承集合List,并且定义一个List集合属性,并添加@Valid注解

@Data
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);
    }

}

集合的校验则变成如下方式

	@PostMapping("saveBatch")
    @ApiOperation("批量保存")
    @LogOperation("批量保存")
    public Result saveBatch(@RequestBody @Validated ValidList list) {
        resultFieldService.saveBatch(list);
        return Result.ok();
    }

2.2 第二种方案

controller上添加@Validated

@RestController
@RequestMapping("search")
@Validated
public class ResultFieldController {
......
}

则集合校验变成如下方式

	@PostMapping("saveBatch")
    @ApiOperation("批量保存")
    @LogOperation("批量保存")
    public Result saveBatch(@RequestBody @Valid List list) {
        resultFieldService.saveBatch(list);
        return Result.ok();
    }

你可能感兴趣的:(SpringBoot,spring,boot,java)