java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > springboot @validated List校验失效

springboot @validated List校验失效问题

作者:我有一只肥螳螂

这篇文章主要介绍了springboot @validated List校验失效问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教

springboot @validated List校验失效

添加依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-validation</artifactId>
</dependency>

@validated 和 @valid 的区别

问题

@Validated用在列表上是不生效的

@ApiOperation(value = "查询列表", notes = "查询列表")
@GetMapping(value = "/test/list")
String listTest( @RequestBody @Validated List<TestListReqDTO> list) {
    return "成功查询";
}
@Data
@ApiModel(value = "TestListReqDTO", description = "列表查询")
public class TestListReqDTO   {

    @ApiModelProperty("id")
    @NotNull(message = "id 不能为空")
    Long id;

    @ApiModelProperty("名称")
    @NotBlank(message = "名称 不能为空")
    String name;
}

解决方案

创建 ValidatedList

@Data
public class ValidatedList<E> implements List<E> , Serializable {
    @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) {
        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.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<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);
    }
}

使用 ValidatedList

@ApiOperation(value = "查询列表", notes = "查询列表")
@GetMapping(value = "/test/list/valid")
String listTest(@RequestBody @Validated ValidatedList<TestListReqDTO> list) {
    return "成功查询";
}

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

您可能感兴趣的文章:
阅读全文