@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 c) {
    return list.addAll(c);
  }

  @Override
  public boolean addAll(int index, Collection 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 allExceptionHandler(MethodArgumentNotValidException e){
        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;
	}

 

你可能感兴趣的:(java)