/** * 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(); }
/** * 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(); }
@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)); }
@ExceptionHandler(ServletRequestBindingException.class) @ResponseStatus(value = HttpStatus.BAD_REQUEST) public Object handServletIllegalException(ServletRequestBindingException e) { return ServerResponse.createByErrorCodeMessage(HttpStatus.BAD_REQUEST.value(), e.getMessage()); }
@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); }
@ExceptionHandler( ServletRequestBindingException.class ) public ResponseEntity servletRequestBindingExceptionHandler ( ServletRequestBindingException e ) { LogUtils.getLogger().error( "error" , e ); return ResponseEntityPro.badRequest( e.getMessage() + ",请检查参数名称是否符合格式." ); }
@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)); }
@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)); }
/** 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); }
@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); }