/** * 处理客户端请求参数错误,返回响应码400 * * @param request 请求对象 * @param exception 异常对象 * @param locale 地理信息 * @return */ @ResponseStatus(value = HttpStatus.BAD_REQUEST) @ExceptionHandler({ InvalidParameterException.class, MissingServletRequestParameterException.class, HttpMessageNotReadableException.class }) @ResponseBody public BaseResponseEntity<?> handleArgumentException( HttpServletRequest request, RuntimeException exception, Locale locale) { return BaseResponseEntity.build( HttpStatus.BAD_REQUEST.value(), APIStatus.INVALID_PARAMETER.getStatus(), APIStatus.INVALID_PARAMETER.name(), exception, request); }
/** 处理参数校验错误 */ @ExceptionHandler({BindException.class}) @ResponseStatus(value = HttpStatus.BAD_REQUEST) @ResponseBody public BaseResponseEntity<List<String>> handleException( BindException exception, HttpServletRequest request) { BindingResult bindingResult = exception.getBindingResult(); List<ObjectError> objectErrors = bindingResult.getAllErrors(); List<String> errorMessages = new LinkedList<String>(); for (ObjectError objectError : objectErrors) { errorMessages.add(objectError.getDefaultMessage()); } return BaseResponseEntity.build( HttpStatus.BAD_REQUEST.value(), APIStatus.INVALID_PARAMETER.getStatus(), APIStatus.INVALID_PARAMETER.name(), errorMessages, request); }