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 {
@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);
}
附:校验基本数据类型和String类型的方法参数时也需要在类上加@Validated
原文地址:https://blog.csdn.net/a1275302036/article/details/134583563
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若转载,请注明出处:http://www.7code.cn/show_14403.html
如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱:suwngjj01@126.com进行投诉反馈,一经查实,立即删除!
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。