SpringBoot校验List失效解决方法

SpringBoot校验List失效解决方法

失效场景示例代码:

1
2
3
4
5
6
7
8
9
10
11
12
@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注解

1
2
3
4
5
6
7
8
9
10
11
12
@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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
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

参考链接

-------------本文结束感谢您的阅读-------------
失败是成功之母,打赏是成功支付