洞察探索如何通过一套代码实现跨平台小程序开发与高效管理,助力企业数字化转型
1018
2022-11-30
使用@Valid 校验嵌套对象
目录@Valid 校验嵌套对象@Valid 嵌套对象验证不成功
@Valid 校验嵌套对象
参考网上的博客:ValidList 能校验list。
通过测试发现,@Valid只能校验一层。比如我这里有个person对象,里面有个ValidList
如果在Controller层加上@Valid 是校验不到ValidList
需要在List
沾点代码:
package com.xiaoming.entity;
import javax.validation.Valid;
import java.util.*;
public class ValidList
@Valid
private List
@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
return list.iterator();
}
@Override
public Object[] toArray() {
return list.toArray();
}
@Override
public
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
http:// 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
return list.listIterator();
}
@Override
public ListIterator
return list.listIterator(index);
}
@Override
public List
return list.subList(fromIndex, toIndex);
}
public List
return list;
}
public void setList(List
this.list = list;
}
}
@PostMapping("/test")
public String testBytes(@Valid @RequestBody ValidList
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
}
@Valid 嵌套对象验证不成功
controller:
@PostMapping("WSendList")
public ApiResult getList( @ApiParam(required = true) @Valid @RequestBody PageListIO
...
return ApiResult.success();
}
PageLhttp://istIO对象:
WSendIO对象:
public class WSendIO implements Serializable{
@NotNull(message = "字段 不能为空")
private Integer isTest;
...
}
这样请求WSendIO中的验证时不会执行的
解决方案:在PageListIO中使用@Valid
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~