文章目录
- 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);}
}
解决方法一:校验效果如下
解决方法二:自定义实现一个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 {@Validprivate List<E> list = new LinkedList<>();@Overridepublic int size() {return list.size();}@Overridepublic boolean isEmpty() {return list.isEmpty();}@Overridepublic boolean contains(Object o) {return list.contains(o);}@Overridepublic Iterator<E> iterator() {return list.iterator();}@Overridepublic Object[] toArray() {return list.toArray();}@Overridepublic <T> T[] toArray(T[] a) {return list.toArray(a);}@Overridepublic boolean add(E e) {return list.add(e);}@Overridepublic boolean remove(Object o) {return list.remove(o);}@Overridepublic boolean containsAll(Collection<?> c) {return list.containsAll(c);}@Overridepublic boolean addAll(Collection<? extends E> c) {return list.addAll(c);}@Overridepublic boolean addAll(int index, Collection<? extends E> c) {return list.addAll(index, c);}@Overridepublic boolean removeAll(Collection<?> c) {return list.removeAll(c);}@Overridepublic boolean retainAll(Collection<?> c) {return list.retainAll(c);}@Overridepublic void clear() {list.clear();}@Overridepublic E get(int index) {return list.get(index);}@Overridepublic E set(int index, E element) {return list.set(index, element);}@Overridepublic void add(int index, E element) {list.add(index, element);}@Overridepublic E remove(int index) {return list.remove(index);}@Overridepublic int indexOf(Object o) {return list.indexOf(o);}@Overridepublic int lastIndexOf(Object o) {return list.lastIndexOf(o);}@Overridepublic ListIterator<E> listIterator() {return list.listIterator();}@Overridepublic ListIterator<E> listIterator(int index) {return list.listIterator(index);}@Overridepublic 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);}
解决方法二:校验效果如下
附:校验基本数据类型和String类型的方法参数时也需要在类上加@Validated
参考链接