@Validated驗證List集合的方法示例
阿新 • • 發佈:2020-05-15
在開發時發現,@Validated只能驗證單個實體類,在驗證List集合時則不生效
@PostMapping(value="/test") public ApiResult getRepaymentPlan(@RequestBody @Validated List<Test> repaymentPlanVOs){ }
經過查資料得知,@valid是可以使用的,我們自定義一個ValidList類來替換List就可以達到驗證的目的,話不多說,直接看程式碼
public class ValidList<E> implements List<E> { @Valid private List<E> 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<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; } }
然後用ValidList類替換我們的List即可
@PostMapping(value="/test") public ApiResult getRepaymentPlan(@RequestBody @Validated ValidList<Test> repaymentPlanVOs){ }
補充,將@Validated返回的異常替換為我們自定義的json格式並返回,只需在當前controller類裡注入一個異常bean,並用@ExceptionHandler來捕獲對應的異常即可。MethodArgumentNotValidException為@Validated返回的異常,這裡只需要捕獲對應異常即可
@ExceptionHandler(value = MethodArgumentNotValidException.class) @ResponseBody public Map<String,Object> allExceptionHandler(MethodArgumentNotValidException e){ Map<String,Object> result = new HashMap<String,Object>(); 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; }
到此這篇關於@Validated驗證List集合的方法示例的文章就介紹到這了,更多相關@Validated驗證List集合內容請搜尋我們以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援我們!