https://www.imooc.com/article/68948
@ExceptionHandler(MethodArgumentNotValidException.class) public Map<String,Object> handleBindException(MethodArgumentNotValidException ex) { FieldError fieldError = ex.getBindingResult().getFieldError(); log.info("参数校验异常:{}({})", fieldError.getDefaultMessage(),fieldError.getField()); Map<String,Object> result = new HashMap<String,Object>(); result.put("respCode", "01002"); result.put("respMsg", fieldError.getDefaultMessage()); return result; }
@ExceptionHandler(value = MethodArgumentNotValidException.class)
public String handleMethodArgumentNotValidException(MethodArgumentNotValidException e) {
StringBuilder sb = new StringBuilder();
List<ObjectError> allErrors = e.getBindingResult().getAllErrors();
String message = allErrors.stream().map(s -> s.getDefaultMessage()).collect(Collectors.joining(";"));
return message;
}
}