@Validated验证List集合的方法示例
在开发时发现,@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集合内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!
赞 (0)