/** * 请求方法的的参数缺失 */ @ExceptionHandler(MissingServletRequestParameterException.class) @ResponseStatus(HttpStatus.BAD_REQUEST) ResponseMessage handleException(MissingServletRequestParameterException exception) { return ResponseMessage .error(HttpStatus.BAD_REQUEST.value(), "参数[" + exception.getParameterName() + "]不能为空"); }
@ExceptionHandler(MissingServletRequestParameterException.class) public ResponseEntity<String> handleMissingParams(MissingServletRequestParameterException ex) { Loggers.SRV_LOG.error("got exception.", ex); String name = ex.getParameterName(); return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("Parameter '" + name + "' is missing"); }
/** * 通过MissingServletRequestParameterException初始化 * * @param cause * MissingServletRequestParameterException */ public MissingServletRequestParameterExceptionInfo(MissingServletRequestParameterException cause) { super(cause, 403); parameterName = cause.getParameterName(); }
/** * 通过MissingServletRequestParameterException初始化 * * @param cause * MissingServletRequestParameterException */ public MissingServletRequestParameterExceptionInfo(MissingServletRequestParameterException cause) { super(cause, 403); parameterName = cause.getParameterName(); }
@ExceptionHandler(MissingServletRequestParameterException.class) public void handleMissingParams(MissingServletRequestParameterException ex) { String name = ex.getParameterName(); System.out.println(name + " parameter is missing"); // Actual exception handling }
@ExceptionHandler @ResponseStatus(HttpStatus.BAD_REQUEST) public Map<String, Object> handleMissingParam(MissingServletRequestParameterException e) { Map<String, Object> result = new HashMap(); result.put("status", Integer.toString(400)); Map<String, String> error = new HashMap(); error.put("param", e.getParameterName()); result.put("error", error); return result; }
/** * 请求方法的的参数缺失 */ @ExceptionHandler(MissingServletRequestParameterException.class) @ResponseStatus(HttpStatus.BAD_REQUEST) ResponseMessage handleException(MissingServletRequestParameterException exception) { return ResponseMessage .error(HttpStatus.BAD_REQUEST.value(), "参数[" + exception.getParameterName() + "]不能为空"); }
@ExceptionHandler(MissingServletRequestParameterException.class) @ResponseStatus(HttpStatus.BAD_REQUEST) public R handleError(MissingServletRequestParameterException e) { log.warn("缺少请求参数", e.getMessage()); String message = String.format("缺少必要的请求参数: %s", e.getParameterName()); return R.fail(ResultCode.PARAM_MISS, message); }
@Override protected ResponseEntity<Object> handleMissingServletRequestParameter(final MissingServletRequestParameterException ex, final HttpHeaders headers, final HttpStatus status, final WebRequest request) { logger.info(ex.getClass().getName()); // final String error = ex.getParameterName() + " parameter is missing"; final ApiError apiError = new ApiError(HttpStatus.BAD_REQUEST, ex.getLocalizedMessage(), error); return new ResponseEntity<Object>(apiError, new HttpHeaders(), apiError.getStatus()); }
@Override protected ResponseEntity<Object> handleMissingServletRequestParameter(MissingServletRequestParameterException ex, HttpHeaders headers, HttpStatus status, WebRequest request) { String name = ex.getParameterName(); CommonResponse<?> body = CommonResponse.fail(CommonResponse.BAD_REQUEST, "缺少请求参数: " + name); return handleExceptionInternal(ex, body, headers, status, request); }
/** * Customize the response for MissingServletRequestParameterException. * @param ex the exception * @param headers the headers to be written to the response * @param status the selected response status * @param request the current request * @return the RestErrorResponse Object */ @Override protected ResponseEntity<Object> handleMissingServletRequestParameter(MissingServletRequestParameterException ex, HttpHeaders headers, HttpStatus status, WebRequest request) { String error = ex.getParameterName() + " parameter is missing"; return buildResponseEntity(new RestErrorResponse(HttpStatus.BAD_REQUEST, error, ex)); }
/** * MissingServletRequestParameterException. * http状态码为400 * * @param ex HttpMessageNotReadableException * @return ModelAndView */ @ExceptionHandler(MissingServletRequestParameterException.class) @ResponseStatus(HttpStatus.BAD_REQUEST) public ModelAndView handleMissingServletRequestParameterException( MissingServletRequestParameterException ex, HttpServletRequest request, HttpServletResponse response) { SystemException systemException = SystemException.create(DefaultErrorCode.MISSING_ARGS) .set("parameter", ex.getParameterName()); return handleSystemException(systemException, request, response); }
/** * Handle MissingServletRequestParameterException. Triggered when a 'required' request parameter is missing. * * @param ex MissingServletRequestParameterException * @param headers HttpHeaders * @param status HttpStatus * @param request WebRequest * @return the ApiError object */ @Override protected ResponseEntity<Object> handleMissingServletRequestParameter( MissingServletRequestParameterException ex, HttpHeaders headers, HttpStatus status, WebRequest request) { String error = ex.getParameterName() + " parameter is missing"; return buildResponseEntity(new ApiError(BAD_REQUEST, error, ex)); }
private HttpResult toHttpResult(Exception e, HttpServletRequest request) { long t0 = System.currentTimeMillis(); if (request == null) { ServletRequestAttributes servlet = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); request = servlet.getRequest(); } String requestId = RestPackAspect.generateRequestId(); String requestPath = request.getRequestURI(); MDC.put("requestId", requestId); MDC.put("requestPath", requestPath); BusinessException be; if (e instanceof MissingServletRequestParameterException) { MissingServletRequestParameterException me = (MissingServletRequestParameterException) e; log.info("missing param: {} in path: {}", me.getParameterName(), request.getPathInfo()); be = new BusinessException(ErrorCodes.NULL_PARAM) .put("key", me.getParameterName()) .setMessage("参数 ${key} 不能为空!"); } else { log.error("[Handled] unknown exception", e); be = new BusinessException(CommonErrorCode.UNKNOWN_ERROR, e) .setMessage(e.getClass().getName() + ": " + e.getMessage()); } HttpResult result = HttpResult.fail(be); result.setRequestId(requestId); long t = System.currentTimeMillis(); result.setServerTime(t); result.setSpendTime(t - t0); return result; }
BusinessException convert(Throwable e) { if (e instanceof BusinessException) { return (BusinessException) e; } if (e instanceof MissingServletRequestParameterException) { MissingServletRequestParameterException me = (MissingServletRequestParameterException) e; String paramKey = me.getParameterName(); String paramType = me.getParameterType(); Logger log = RestPackAspect.getLog(); if (log != null && log.isInfoEnabled()) { log.info("miss param, key = {}, type = {}", paramKey, paramType); } return new BusinessException(ErrorCodes.NULL_PARAM) .put("key", paramKey); } // Error 没有办法拦截,这里只能日志记录异常信息。 if (e instanceof Error) { e.printStackTrace(); Logger log = RestPackAspect.getLog(); if (log != null) { log.error(e.getMessage(), e); } } return new BusinessException(ErrorCodes.UNKNOWN_ERROR, e) .setMessage(e.getClass().getName() + ": " + e.getMessage()); }
@Override public void convert(final MissingServletRequestParameterException exception) { this.parameter = exception.getParameterName(); setMessage(getMessageSource().getMessage(MISSING_REQUEST_PARAMETER, this.getParameter())); }
StringBuilder builder = new StringBuilder("参数字段"); MissingServletRequestParameterException ex = (MissingServletRequestParameterException) exception; builder.append(ex.getParameterName()); builder.append("校验不通过"); failedResponseBuilder.exception(builder.toString()).msg(ex.getMessage());
@Override protected ResponseEntity<Object> handleMissingServletRequestParameter(MissingServletRequestParameterException ex, HttpHeaders headers, HttpStatus status, WebRequest request) { MultibankingError error = MultibankingError.builder().status(HttpStatus.BAD_REQUEST) .text(ErrorConstants.ERR_PARAMETER) .renderedMessage(ErrorConstants.ERR_PARAMETER_MESSAGE) .exception(ex).build(); error.getParams().put(ErrorConstants.ERR_PARAMETER_NAME_KEY, ex.getParameterName()); error.getParams().put(ErrorConstants.ERR_PARAMETER_TYPE_KEY, ex.getParameterType()); return handleExceptionInternal(ex, error, headers, error.getStatus(), request); }
@Override protected ResponseEntity<Object> handleMissingServletRequestParameter(MissingServletRequestParameterException ex, HttpHeaders headers, HttpStatus status, WebRequest request) { MultibankingError error = MultibankingError.builder().status(HttpStatus.BAD_REQUEST) .text(ErrorConstants.ERR_PARAMETER) .renderedMessage(ErrorConstants.ERR_PARAMETER_MESSAGE) .exception(ex).build(); error.getParams().put(ErrorConstants.ERR_PARAMETER_NAME_KEY, ex.getParameterName()); error.getParams().put(ErrorConstants.ERR_PARAMETER_TYPE_KEY, ex.getParameterType()); return handleExceptionInternal(ex, error, headers, error.getStatus(), request); }
String name = actualException.getParameterName(); String type = actualException.getParameterType();