springboot @validated List 校验失效

添加依赖

<dependency>
    <groupId>org.springframework.bootgroupId>
    <artifactId>spring-boot-starter-validationartifactId>
dependency>

@validated 和 @valid 的区别

  1. @Validated:提供了一个分组功能,可以在入参验证时,根据不同的分组采用不同的验证机制。没有添加分组属性时,默认验证没有分组的验证属性。
  2. @Validated:用在类型、方法和方法参数上。但不能用于成员属性(field)
  3. @Valid:可以用在方法、构造函数、方法参数和成员属性(field)上,所以可以用@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 "成功查询";
}

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