@Valid 校验嵌套对象
参考网上的博客:ValidList 能校验list。
通过测试发现,@Valid只能校验一层。比如我这里有个person对象,里面有个ValidList<Teacher> 属性,Teacher对象里面有个List<Student> 属性。
如果在Controller层加上@Valid 是校验不到ValidList<Student> 属性的。
需要在List<Teacher> 属性前面加上 List<@Valid Teacher>
沾点代码:
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
|
package com.xiaoming.entity; import javax.validation.Valid; import java.util.*; public class ValidList<E> implements List<E> { @Valid private List<E> list = new ArrayList<>(); @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); } public List<E> getList() { return list; } public void setList(List<E> list) { this .list = list; } } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
@PostMapping ( "/test" ) public String testBytes( @Valid @RequestBody ValidList<Persion> persions){ return "success" ; } @Data public class Persion { @NotNull (message = "name != null" ) private String name; @NotNull (message = "teacherList != null" ) private ValidList< @Valid Teacher> teacherList; } @Data public class Teacher { @NotNull private String name; @NotNull private List<Student> students; } |
@Valid 嵌套对象验证不成功
controller:
1
2
3
4
5
|
@PostMapping ( "WSendList" ) public ApiResult getList( @ApiParam (required = true ) @Valid @RequestBody PageListIO<WSendIO> body) { ... return ApiResult.success(); } |
PageListIO对象:
WSendIO对象:
1
2
3
4
5
|
public class WSendIO implements Serializable{ @NotNull (message = "字段 不能为空" ) private Integer isTest; ... } |
这样请求WSendIO中的验证时不会执行的
解决方案:在PageListIO中使用@Valid
以上为个人经验,希望能给大家一个参考,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/chenmingzhong99/article/details/104560207