javavalidation分组嵌套校验

更新时间:2023-05-09 18:08:32 阅读: 评论:0

javavalidation分组嵌套校验分组接⼝
public interface ValidGroup extends Default {
interface Crud extends ValidGroup{
interface Create extends Crud{
}
interface Update extends Crud{
}
interface Query extends Crud{
}
interface Delete extends Crud{
}
}
}
1、Controller参数为对象
1、Controller层上加上
@Validated
2、⽅法demo
public R edit(@Validated(value = ValidGroup.Crud.Update.class) @RequestBody InputQcBean inputQcBean) 3、bean
@Data
public class InputQcBean {
@NotNull
@ApiModelProperty(value = "质检单ID")
private String id;
@Valid
@ApiModelProperty(value = "分析项结果")
private List<QualityData> list;
}
嵌套bean
public class QualityData extends Model<QualityData> {
@ApiModelProperty(value = "⾃增ID ,录⼊时候不需要,编辑的时候需要上传")
@Null(groups = ValidGroup.Crud.Create.class,message = "插⼊时ID为空")
@NotNull(groups = ValidGroup.Crud.Update.class, message = "编辑时ID不能为空")
@TableId(type = IdType.AUTO)
private Integer id;
@NotNull(message = "分析项结果不能为空")
private String result;
}
2、Controller层为对象数组
经本⼈测试
如果controller写成如下,所有效验都⽆效了
public R input(@RequestBody @Validated(value = ValidGroup.Crud.Create.class) List<InputQcBean> list)
如果变成这样,嵌套类包括本类,默认效验⽣效,分组效验⽆效
public R input(@RequestBody @Validated(value = ValidGroup.Crud.Create.class) @Valid List<InputQcBean> list)要使分组和默认同时⽣效
第⼀种⽅法:再封装⼀个对象把 List<InputQcBean> list 封装进去
第⼆种⽅法:⾃定义List 代替java.util.List
Controller写法
public R input(@RequestBody @Validated(value = ValidGroup.Crud.Create.class) ValidList<InputQcBean> list) ValidList
/**
* @author cmy
* @date 2021/8/31 19:59
*/
@Data
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) {
ains(o);
ains(o);
}
@Override
public Iterator<E> iterator() {
return list.iterator();
}
@Override
public Object[] toArray() {
Array();
}
@Override
public <T> T[] toArray(T[] a) {
Array(a);
}
@Override
public boolean add(E e) {
return list.add(e);
}
@Override
public boolean remove(Object o) {
ve(o);
}
@Override
public boolean containsAll(Collection<?> c) {
ainsAll(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) {
veAll(c);
}
@Override
public boolean retainAll(Collection<?> c) {
ainAll(c);
}
@Override
public void clear() {
list.clear();
}
@Override
public E get(int index) {
(index);
}
@Override
public E t(int index, E element) {
return list.t(index,element);
return list.t(index,element);
}
@Override
public void add(int index, E element) {
list.add(index,element);
}
@Override
public E remove(int index) {
ve(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);
}
}

本文发布于:2023-05-09 18:08:32,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/82/566181.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:分组   对象   效验
相关文章
留言与评论(共有 0 条评论)
   
验证码:
推荐文章
排行榜
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图