@Validated验证List集合的方法示例
在开发时发现,@Validated只能验证单个实体类,在验证List集合时则不生效
@PostMapping(value="/test") publicApiResultgetRepaymentPlan(@RequestBody@ValidatedListrepaymentPlanVOs){ }
经过查资料得知,@valid是可以使用的,我们自定义一个ValidList类来替换List就可以达到验证的目的,话不多说,直接看代码
publicclassValidListimplementsList { @Valid privateList list=newLinkedList<>(); @Override publicintsize(){ returnlist.size(); } @Override publicbooleanisEmpty(){ returnlist.isEmpty(); } @Override publicbooleancontains(Objecto){ returnlist.contains(o); } @Override publicIterator iterator(){ returnlist.iterator(); } @Override publicObject[]toArray(){ returnlist.toArray(); } @Override public T[]toArray(T[]a){ returnlist.toArray(a); } @Override publicbooleanadd(Ee){ returnlist.add(e); } @Override publicbooleanremove(Objecto){ returnlist.remove(o); } @Override publicbooleancontainsAll(Collection>c){ returnlist.containsAll(c); } @Override publicbooleanaddAll(Collectionc){ returnlist.addAll(c); } @Override publicbooleanaddAll(intindex,Collectionc){ returnlist.addAll(index,c); } @Override publicbooleanremoveAll(Collection>c){ returnlist.removeAll(c); } @Override publicbooleanretainAll(Collection>c){ returnlist.retainAll(c); } @Override publicvoidclear(){ list.clear(); } @Override publicEget(intindex){ returnlist.get(index); } @Override publicEset(intindex,Eelement){ returnlist.set(index,element); } @Override publicvoidadd(intindex,Eelement){ list.add(index,element); } @Override publicEremove(intindex){ returnlist.remove(index); } @Override publicintindexOf(Objecto){ returnlist.indexOf(o); } @Override publicintlastIndexOf(Objecto){ returnlist.lastIndexOf(o); } @Override publicListIterator listIterator(){ returnlist.listIterator(); } @Override publicListIterator listIterator(intindex){ returnlist.listIterator(index); } @Override publicList subList(intfromIndex,inttoIndex){ returnlist.subList(fromIndex,toIndex); } publicList getList(){ returnlist; } publicvoidsetList(List list){ this.list=list; } }
然后用ValidList类替换我们的List即可
@PostMapping(value="/test") publicApiResultgetRepaymentPlan(@RequestBody@ValidatedValidListrepaymentPlanVOs){ }
补充,将@Validated返回的异常替换为我们自定义的json格式并返回,只需在当前controller类里注入一个异常bean,并用@ExceptionHandler来捕获对应的异常即可。MethodArgumentNotValidException为@Validated返回的异常,这里只需要捕获对应异常即可
@ExceptionHandler(value=MethodArgumentNotValidException.class) @ResponseBody publicMapallExceptionHandler(MethodArgumentNotValidExceptione){ Map result=newHashMap (); MethodArgumentNotValidExceptionex=(MethodArgumentNotValidException)e; BindingResultbindingResult=ex.getBindingResult(); StringBuildererrMsg=newStringBuilder(bindingResult.getFieldErrors().size()*16); errMsg.append("Invalidrequest:"); for(inti=0;i 0){ errMsg.append(","); } FieldErrorerror=bindingResult.getFieldErrors().get(i); errMsg.append(error.getField()+":"+error.getDefaultMessage()); } result.put("res_code","400"); result.put("res_mess",errMsg.toString()); returnresult; }
到此这篇关于@Validated验证List集合的方法示例的文章就介绍到这了,更多相关@Validated验证List集合内容请搜索毛票票以前的文章或继续浏览下面的相关文章希望大家以后多多支持毛票票!