@Validated验证List集合的方法示例

网友投稿 870 2022-12-07


@Validated验证List集合的方法示例

在开发时发现,@Validated只能验证单个实体类,在验证List集合时则不生效

@PostMapping(value="/test")

public ApiResult getRepaymentPlan(@RequestBody @Validated List repaymentPlanVOs){

}

经过查资料得知,@valid是可以使用的,我们自定义一个ValidList类来替换List就可以达到验证的目的,话不多说,直接看代码

public class ValidList implements List {

@Valid

private List 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 iterator() {

return list.iterator();

}

@Override

public Object[] toArray() {

return list.toArray();

}

@Override

public 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 listIterator() {

return list.listIterator();

}

@Override

public ListIterator listIterator(int index) {

return list.listIterator(index);

}

@Override

public List subList(int fromIndex, int toIndex) {

return list.subList(fromIndex, toIndex);

}

public List getList() {

return list;

}

public void setList(List list) {

this.list = list;

}

}

然后用ValidList类替换我们的List即可

@PostMapping(value="/test")

public ApiResult getRepaymentPlan(@RequestBody @Validated ValidList repaymentPlanVOs){

}

补充,将@Validated返回的异常替换为我们自定义的json格式并返回,只需在当前controller类里注入一个异常bean,并用@ExceptionHandler来捕获对应的异常即可。MethodArgumentNotValidException为@Validated返回的异常,这里只需要捕获对应异常即可

@ExceptionHandler(value = MethodArgumentNotValidException.class)

@ResponseBody

public Map

Map result = new HashMap();

MethodArgumentNotValidException ex = (MethodArgumentNotValidException)e;

BindingResult bindingResult = ex.getBindingResult();

StringBuilder errMsg = new StringBuilder(bindingResult.getFieldErrors().size() * 16);

errMsg.append("Invalid request:");

for (int i = 0 ; i < bindingResult.getFieldErrors().size() ; i++) {

if(i > 0) {

errMsg.append(",");

}

FieldError error = bindingResult.getFieldErrors().get(i);

errMsg.append(error.getField()+":"+error.getDefaultMessage());

}

result.put("res_code","400");

result.put("res_mess",errMsg.toString());

return result;

}

Map result = new HashMap();

MethodArgumentNotValidException ex = (MethodArgumentNotValidException)e;

BindingResult bindingResult = ex.getBindingResult();

StringBuilder errMsg = new StringBuilder(bindingResult.getFieldErrors().size() * 16);

errMsg.append("Invalid request:");

for (int i = 0 ; i < bindingResult.getFieldErrors().size() ; i++) {

if(i > 0) {

errMsg.append(",");

}

FieldError error = bindingResult.getFieldErrors().get(i);

errMsg.append(error.getField()+":"+error.getDefaultMessage());

}

result.put("res_code","400");

result.put("res_mess",errMsg.toString());

return result;

}


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:java Beanutils.copyProperties( )用法详解
下一篇:深入理解java中Arrays.sort()的用法
相关文章

 发表评论

暂时没有评论,来抢沙发吧~