/** * Invoked when a named value is required, but {@link #resolveName(String, MethodParameter, NativeWebRequest)} * returned {@code null} and there is no default value. Subclasses typically throw an exception in this case. * @param name the name for the value * @param parameter the method parameter */ protected void handleMissingValue(String name, MethodParameter parameter) throws ServletException { throw new ServletRequestBindingException("Missing argument '" + name + "' for method parameter of type " + parameter.getNestedParameterType().getSimpleName()); }
/** * 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, @Nullable Object handler) throws IOException { response.sendError(HttpServletResponse.SC_BAD_REQUEST, ex.getMessage()); return new ModelAndView(); }
@Override protected void handleMissingValue(String name, MethodParameter parameter) throws ServletException { throw new ServletRequestBindingException("Missing request attribute '" + name + "' of type " + parameter.getNestedParameterType().getSimpleName()); }
/** * 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, @Nullable Object handler) throws IOException { response.sendError(HttpServletResponse.SC_BAD_REQUEST, ex.getMessage()); return new ModelAndView(); }
@Override protected void handleMissingValue(String name, MethodParameter parameter) throws ServletException { throw new ServletRequestBindingException("Missing session attribute '" + name + "' of type " + parameter.getNestedParameterType().getSimpleName()); }
@Test public void resolve() throws Exception { MethodParameter param = initMethodParameter(0); try { testResolveArgument(param); fail("Should be required by default"); } catch (ServletRequestBindingException ex) { assertTrue(ex.getMessage().startsWith("Missing ")); } Foo foo = new Foo(); this.webRequest.setAttribute("foo", foo, getScope()); assertSame(foo, testResolveArgument(param)); }
@RequestMapping("/") public void handleRequest() throws Exception { throw new ServletRequestBindingException("message"); } }
@ExceptionHandler(ServletRequestBindingException.class) @ResponseStatus(value = HttpStatus.BAD_REQUEST) public Object handServletIllegalException(ServletRequestBindingException e) { return ServerResponse.createByErrorCodeMessage(HttpStatus.BAD_REQUEST.value(), e.getMessage()); }
@RequestMapping("/") public void handleRequest() throws Exception { throw new IllegalStateException(new ServletRequestBindingException("message")); } }
@ExceptionHandler(value = ServletRequestBindingException.class) public ResponseEntity servletRequestBindingException(ServletRequestBindingException ex, HandlerMethod handlerMethod) { log.warn("Validation exception handled in service: {}, message: {}", handlerMethod.getMethod().getDeclaringClass().getSimpleName(), ex.getMessage()); return new ResponseEntity<>(getTppMessages(FORMAT_ERROR), HttpStatus.BAD_REQUEST); }
/** * Invoked when a named value is required, but {@link #resolveName(String, MethodParameter, NativeWebRequest)} * returned {@code null} and there is no default value. Subclasses typically throw an exception in this case. * @param name the name for the value * @param parameter the method parameter */ protected void handleMissingValue(String name, MethodParameter parameter) throws ServletException { throw new ServletRequestBindingException("Missing argument '" + name + "' for method parameter of type " + parameter.getNestedParameterType().getSimpleName()); }
@ExceptionHandler( ServletRequestBindingException.class ) public ResponseEntity servletRequestBindingExceptionHandler ( ServletRequestBindingException e ) { LogUtils.getLogger().error( "error" , e ); return ResponseEntityPro.badRequest( e.getMessage() + ",请检查参数名称是否符合格式." ); }
@Override protected void handleMissingValue(String name, MethodParameter parameter) throws ServletException { throw new ServletRequestBindingException("Missing session attribute '" + name + "' of type " + parameter.getNestedParameterType().getSimpleName()); }
@ExceptionHandler(value = ServletRequestBindingException.class) public ResponseEntity servletRequestBindingException(ServletRequestBindingException ex, HandlerMethod handlerMethod) { log.warn("Validation exception handled in service: {}, message: {}", handlerMethod.getMethod().getDeclaringClass().getSimpleName(), ex.getMessage()); return responseErrorMapper.generateErrorResponse(createMessageError(FORMAT_ERROR)); }
@Override protected void handleMissingValue(String name, MethodParameter parameter) throws ServletException { throw new ServletRequestBindingException("Missing request attribute '" + name + "' of type " + parameter.getNestedParameterType().getSimpleName()); }
@ExceptionHandler(value = ServletRequestBindingException.class) public ResponseEntity servletRequestBindingException(ServletRequestBindingException ex, HandlerMethod handlerMethod) { log.warn("Validation exception handled in service: {}, message: {}", handlerMethod.getMethod().getDeclaringClass().getSimpleName(), ex.getMessage()); return responseErrorMapper.generateErrorResponse(createMessageError(FORMAT_ERROR)); }
protected final T parse(String name, String parameter) throws ServletRequestBindingException { validateRequiredParameter(name, parameter); try { return doParse(parameter); } catch (NumberFormatException ex) { throw new ServletRequestBindingException( "Required " + getType() + " parameter '" + name + "' with value of '" + parameter + "' is not a valid number", ex); } }
/** Servlet例外 */ @ExceptionHandler(ServletRequestBindingException.class) public ResponseEntity<Map<String, String[]>> handleServletRequestBinding(ServletRequestBindingException e) { log.warn(e.getMessage()); return new ErrorHolder(msg, locale(), "error.ServletRequestBinding").result(HttpStatus.BAD_REQUEST); }
if (found) { String paramType = parameter.getNestedParameterType().getName(); throw new ServletRequestBindingException( "Found more than one match for URI path parameter '" + name + "' for parameter type [" + paramType + "]. Use 'pathVar' attribute to disambiguate.");
@ExceptionHandler(ServletRequestBindingException.class) public ResponseEntity<?> servletRequestBindingException(ServletRequestBindingException e) { ErrorDetails errorDetails = new ErrorDetails(); errorDetails.setErrorMessage(e.getMessage()); errorDetails.setDevErrorMessage(getStackTraceAsString(e)); return new ResponseEntity<>(errorDetails, HttpStatus.BAD_REQUEST); }