SpringBoot校验List失效解决方法

文章目录

    • SpringBoot校验List失效解决方法
    • 附:校验基本数据类型和String类型的方法参数时也需要在类上加@Validated

SpringBoot校验List失效解决方法

失效场景示例代码:

@RestController
@RequestMapping("/v1/jx/flowSummary")
@Slf4j
public class JxFlowSummaryController {

    @Operation(summary = "批量修改原始得分")
    @PostMapping("/updateScore")
    public ResponseDto batchUpdateScore(@RequestBody @Valid List<BatchUpdateScoreDto> dtoList) {
        // jxFlowSummaryService.batchUpdateScore(dtoList);
        return ResponseUtil.wrapSuccess(null);
    }
}

解决方法一:在controller上加上@Validated注解

@RestController
@RequestMapping("/v1/jx/flowSummary")
@Slf4j
@Validated
public class JxFlowSummaryController {

    @Operation(summary = "批量修改原始得分")
    @PostMapping("/updateScore")
    public ResponseDto batchUpdateScore(@RequestBody @Valid List<BatchUpdateScoreDto> dtoList){
        return ResponseUtil.wrapSuccess(null);
    }
}

解决方法一:校验效果如下
SpringBoot校验List失效解决方法_第1张图片

解决方法二:自定义实现一个ValidatedList,接口参数使用ValidatedList

import lombok.Data;
import javax.validation.Valid;
import java.io.Serializable;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;

/**
 * 自定义校验list
 *
 * @author liquanhong
 * @createTime 2023/11/23
 */
@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);
    }
}

// List改为了 ValidatedList
@Operation(summary = "批量修改原始得分")
 @PostMapping("/updateScore")
 public ResponseDto batchUpdateScore(@RequestBody @Valid ValidatedList<BatchUpdateScoreDto> dtoList){
     return ResponseUtil.wrapSuccess(null);
 }

解决方法二:校验效果如下
SpringBoot校验List失效解决方法_第2张图片

附:校验基本数据类型和String类型的方法参数时也需要在类上加@Validated

参考链接

你可能感兴趣的:(spring,boot,list,后端)