@Override protected void handleMissingValue(String name, MethodParameter parameter) throws ServletRequestBindingException { throw new MissingPathVariableException(name, parameter); }
/** * Handle the case when a declared path variable does not match any extracted URI variable. * <p>The default implementation sends an HTTP 500 error, and returns an empty {@code ModelAndView}. * Alternatively, a fallback view could be chosen, or the MissingPathVariableException * could be rethrown as-is. * @param ex the MissingPathVariableException to be handled * @param request current HTTP request * @param response current HTTP response * @param handler the executed handler * @return an empty ModelAndView indicating the exception was handled * @throws IOException potentially thrown from {@link HttpServletResponse#sendError} * @since 4.2 */ protected ModelAndView handleMissingPathVariable(MissingPathVariableException ex, HttpServletRequest request, HttpServletResponse response, @Nullable Object handler) throws IOException { response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, ex.getMessage()); return new ModelAndView(); }
StringBuilder builder = new StringBuilder("路径字段"); MissingPathVariableException ex = (MissingPathVariableException) exception; builder.append(ex.getVariableName()); builder.append("校验不通过"); failedResponseBuilder.exception(builder.toString()).msg(ex.getMessage()); return failedResponseBuilder; } else if (exception instanceof ConstraintViolationException) {
@Override protected ResponseEntity<Object> handleMissingPathVariable(MissingPathVariableException ex, HttpHeaders headers, HttpStatus status, WebRequest request) { String name = ex.getVariableName(); CommonResponse<?> body = CommonResponse.fail(CommonResponse.INTERNAL_ERROR, "缺少路径参数: " + name); return handleExceptionInternal(ex, body, headers, status, request); }
/** * Customize the response for MissingPathVariableException. * @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 * @since 4.2 */ @Override protected ResponseEntity<Object> handleMissingPathVariable(MissingPathVariableException ex, HttpHeaders headers, HttpStatus status, WebRequest request) { String error = "Path Variable : " + ex.getVariableName() + " is missing"; return buildResponseEntity(new RestErrorResponse(HttpStatus.BAD_REQUEST, error, ex)); }
@Override protected void handleMissingValue(String name, MethodParameter parameter) throws ServletRequestBindingException { throw new MissingPathVariableException(name, parameter); }
/** * Handle the case when a declared path variable does not match any extracted URI variable. * <p>The default implementation sends an HTTP 500 error, and returns an empty {@code ModelAndView}. * Alternatively, a fallback view could be chosen, or the MissingPathVariableException * could be rethrown as-is. * @param ex the MissingPathVariableException to be handled * @param request current HTTP request * @param response current HTTP response * @param handler the executed handler * @return an empty ModelAndView indicating the exception was handled * @throws IOException potentially thrown from {@link HttpServletResponse#sendError} * @since 4.2 */ protected ModelAndView handleMissingPathVariable(MissingPathVariableException ex, HttpServletRequest request, HttpServletResponse response, @Nullable Object handler) throws IOException { response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, ex.getMessage()); return new ModelAndView(); }
@Test public void missingPathVariable() throws NoSuchMethodException { Method method = getClass().getDeclaredMethod("handle", String.class); MethodParameter parameter = new MethodParameter(method, 0); Exception ex = new MissingPathVariableException("param", parameter); testException(ex); }
/** * Handle the case when an unrecoverable binding exception occurs - e.g. required header, required cookie. * <p>The default implementation sends an HTTP 400 error, and returns an empty {@code ModelAndView}. * Alternatively, a fallback view could be chosen, or the exception could be rethrown as-is. * @param ex the exception to be handled * @param request current HTTP request * @param response current HTTP response * @param handler the executed handler * @return an empty ModelAndView indicating the exception was handled * @throws IOException potentially thrown from {@link HttpServletResponse#sendError} */ protected ModelAndView handleServletRequestBindingException(ServletRequestBindingException ex, HttpServletRequest request, HttpServletResponse response, Object handler) throws IOException { response.sendError(HttpServletResponse.SC_BAD_REQUEST, ex.getMessage()); return new ModelAndView(); }
@Test public void handleMissingPathVariable() throws NoSuchMethodException { Method method = getClass().getMethod("handle", String.class); MethodParameter parameter = new MethodParameter(method, 0); MissingPathVariableException ex = new MissingPathVariableException("foo", parameter); ModelAndView mav = exceptionResolver.resolveException(request, response, null, ex); assertNotNull("No ModelAndView returned", mav); assertTrue("No Empty ModelAndView returned", mav.isEmpty()); assertEquals("Invalid status code", 500, response.getStatus()); assertEquals("Missing URI template variable 'foo' for method parameter of type String", response.getErrorMessage()); }
@Override protected void handleMissingValue(String name, MethodParameter parameter) throws ServletRequestBindingException { throw new MissingPathVariableException(name, parameter); }