/** * Handle the case where no request handler method was found for the particular HTTP request method. * <p>The default implementation logs a warning, sends an HTTP 405 error, sets the "Allow" header, * and returns an empty {@code ModelAndView}. Alternatively, a fallback view could be chosen, * or the HttpRequestMethodNotSupportedException could be rethrown as-is. * @param ex the HttpRequestMethodNotSupportedException to be handled * @param request current HTTP request * @param response current HTTP response * @param handler the executed handler, or {@code null} if none chosen * at the time of the exception (for example, if multipart resolution failed) * @return an empty ModelAndView indicating the exception was handled * @throws IOException potentially thrown from {@link HttpServletResponse#sendError} */ protected ModelAndView handleHttpRequestMethodNotSupported(HttpRequestMethodNotSupportedException ex, HttpServletRequest request, HttpServletResponse response, @Nullable Object handler) throws IOException { String[] supportedMethods = ex.getSupportedMethods(); if (supportedMethods != null) { response.setHeader("Allow", StringUtils.arrayToDelimitedString(supportedMethods, ", ")); } response.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED, ex.getMessage()); return new ModelAndView(); }
@ExceptionHandler(HttpRequestMethodNotSupportedException.class) public ResponseEntity<OAuth2Exception> handleHttpRequestMethodNotSupportedException(HttpRequestMethodNotSupportedException e) throws Exception { if (logger.isInfoEnabled()) { logger.info("Handling error: " + e.getClass().getSimpleName() + ", " + e.getMessage()); } return getExceptionTranslator().translate(e); }
/** * Customize the response for HttpRequestMethodNotSupportedException. * <p>This method logs a warning, sets the "Allow" header, and delegates to * {@link #handleExceptionInternal}. * @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 a {@code ResponseEntity} instance */ protected ResponseEntity<Object> handleHttpRequestMethodNotSupported( HttpRequestMethodNotSupportedException ex, HttpHeaders headers, HttpStatus status, WebRequest request) { pageNotFoundLogger.warn(ex.getMessage()); Set<HttpMethod> supportedMethods = ex.getSupportedHttpMethods(); if (!CollectionUtils.isEmpty(supportedMethods)) { headers.setAllow(supportedMethods); } return handleExceptionInternal(ex, null, headers, status, request); }
@Override protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { Assert.state(this.target != null, "No HttpRequestHandler available"); LocaleContextHolder.setLocale(request.getLocale()); try { this.target.handleRequest(request, response); } catch (HttpRequestMethodNotSupportedException ex) { String[] supportedMethods = ex.getSupportedMethods(); if (supportedMethods != null) { response.setHeader("Allow", StringUtils.arrayToDelimitedString(supportedMethods, ", ")); } response.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED, ex.getMessage()); } finally { LocaleContextHolder.resetLocaleContext(); } }
/** * Handle the case where no request handler method was found for the particular HTTP request method. * <p>The default implementation logs a warning, sends an HTTP 405 error, sets the "Allow" header, * and returns an empty {@code ModelAndView}. Alternatively, a fallback view could be chosen, * or the HttpRequestMethodNotSupportedException could be rethrown as-is. * @param ex the HttpRequestMethodNotSupportedException to be handled * @param request current HTTP request * @param response current HTTP response * @param handler the executed handler, or {@code null} if none chosen * at the time of the exception (for example, if multipart resolution failed) * @return an empty ModelAndView indicating the exception was handled * @throws IOException potentially thrown from {@link HttpServletResponse#sendError} */ protected ModelAndView handleHttpRequestMethodNotSupported(HttpRequestMethodNotSupportedException ex, HttpServletRequest request, HttpServletResponse response, @Nullable Object handler) throws IOException { String[] supportedMethods = ex.getSupportedMethods(); if (supportedMethods != null) { response.setHeader("Allow", StringUtils.arrayToDelimitedString(supportedMethods, ", ")); } response.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED, ex.getMessage()); return new ModelAndView(); }
/** * Customize the response for HttpRequestMethodNotSupportedException. * <p>This method logs a warning, sets the "Allow" header, and delegates to * {@link #handleExceptionInternal}. * @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 a {@code ResponseEntity} instance */ protected ResponseEntity<Object> handleHttpRequestMethodNotSupported( HttpRequestMethodNotSupportedException ex, HttpHeaders headers, HttpStatus status, WebRequest request) { pageNotFoundLogger.warn(ex.getMessage()); Set<HttpMethod> supportedMethods = ex.getSupportedHttpMethods(); if (!CollectionUtils.isEmpty(supportedMethods)) { headers.setAllow(supportedMethods); } return handleExceptionInternal(ex, null, headers, status, request); }
@Override protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { Assert.state(this.target != null, "No HttpRequestHandler available"); LocaleContextHolder.setLocale(request.getLocale()); try { this.target.handleRequest(request, response); } catch (HttpRequestMethodNotSupportedException ex) { String[] supportedMethods = ex.getSupportedMethods(); if (supportedMethods != null) { response.setHeader("Allow", StringUtils.arrayToDelimitedString(supportedMethods, ", ")); } response.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED, ex.getMessage()); } finally { LocaleContextHolder.resetLocaleContext(); } }
@ExceptionHandler(HttpRequestMethodNotSupportedException.class) public ResponseEntity<OAuth2Exception> handleMethodNotSupportedException(HttpRequestMethodNotSupportedException e) throws Exception { logger.info("Handling error: " + e.getClass().getSimpleName() + ", " + e.getMessage()); ResponseEntity<OAuth2Exception> result = exceptionTranslator.translate(e); if (HttpMethod.POST.matches(e.getMethod())) { OAuth2Exception cause = new OAuth2Exception("Parameters must be passed in the body of the request", result.getBody().getCause()) { public String getOAuth2ErrorCode() { return "query_string_not_allowed"; } public int getHttpErrorCode() { return NOT_ACCEPTABLE.value(); } }; result = new ResponseEntity<>(cause, result.getHeaders(), NOT_ACCEPTABLE); } return result; }
@Override public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException { HttpServletRequest request = (HttpServletRequest) req; HttpServletResponse response = (HttpServletResponse) res; try { super.doFilter(request, response, chain); } catch (RequestRejectedException e) { REQUEST_REJECTED_EXCEPTION_HANDLER.handle(request, response, e.getMessage(), HttpStatus.BAD_REQUEST); } catch (HttpRequestMethodNotSupportedException e) { REQUEST_REJECTED_EXCEPTION_HANDLER.handle(request, response, e.getMessage(), HttpStatus.METHOD_NOT_ALLOWED); } } }
/** * 请求方式不对 * @param e * @return */ @ExceptionHandler(HttpRequestMethodNotSupportedException.class) public ResponseEntity<String> handleRequestMethodException(HttpRequestMethodNotSupportedException e){ ResponseEntity responseEntity = new ResponseEntity(e.getMessage(),HttpStatus.BAD_REQUEST); return responseEntity; }
@Override public ResponseEntity<ErrorMessage> handleException(HttpRequestMethodNotSupportedException ex, HttpServletRequest req) { LOG.warn(ex.getMessage()); return super.handleException(ex, req); }
@ExceptionHandler(HttpRequestMethodNotSupportedException.class) @ResponseBody @ResponseStatus(HttpStatus.METHOD_NOT_ALLOWED) public Error processMethodNotSupportedException(HttpRequestMethodNotSupportedException exception) { return new Error(ErrorConstants.ERR_METHOD_NOT_SUPPORTED, exception.getMessage()); }
/** * 不支持当前请求方法 * * @param e 异常信息 * @return */ @ResponseStatus(HttpStatus.METHOD_NOT_ALLOWED) @ExceptionHandler(HttpRequestMethodNotSupportedException.class) public BaseMessage handleHttpRequestMethodNotSupportedException(HttpRequestMethodNotSupportedException e) { return pubTools.genNoMsg(StatusCode.METHOD_NOT_ALLOWED, e.getMessage()); }
/** *不支持的request请求异常 * @param e * @return */ @ExceptionHandler(HttpRequestMethodNotSupportedException.class) public ResultMap handlerRequest405(HttpRequestMethodNotSupportedException e) { logger.error(e.getMessage(), e); return ResultMap.error(405, "不支持的request方法请求异常"); }
@ExceptionHandler(HttpRequestMethodNotSupportedException.class) @ResponseStatus(value = HttpStatus.METHOD_NOT_ALLOWED) public Object handServletIllegalException(HttpRequestMethodNotSupportedException e) { return ServerResponse.createByErrorCodeMessage(HttpStatus.METHOD_NOT_ALLOWED.value(), e.getMessage()); }
@ExceptionHandler(HttpRequestMethodNotSupportedException.class) @ResponseBody @ResponseStatus(HttpStatus.METHOD_NOT_ALLOWED) public Error processMethodNotSupportedException(HttpRequestMethodNotSupportedException exception) { return new Error(ErrorConstants.ERR_METHOD_NOT_SUPPORTED, exception.getMessage()); }
@ExceptionHandler(HttpRequestMethodNotSupportedException.class) public ResponseEntity<OAuth2Exception> handleHttpRequestMethodNotSupportedException(HttpRequestMethodNotSupportedException e) throws Exception { if (logger.isInfoEnabled()) { logger.info("Handling error: " + e.getClass().getSimpleName() + ", " + e.getMessage()); } return getExceptionTranslator().translate(e); }
@ResponseStatus(HttpStatus.METHOD_NOT_ALLOWED) @ExceptionHandler({HttpRequestMethodNotSupportedException.class}) public ResponseEntity handleHttpRequestMethodNotSupportedException(HttpRequestMethodNotSupportedException e) { LOGGER.error("不支持当前请求方法", e); Response response = Response.failure(DefaultError.METHOD_NOT_SUPPORTED); response.setExtMessage(e.getMessage()); return new ResponseEntity<>(response, HttpStatus.METHOD_NOT_ALLOWED); }
@ResponseBody @ExceptionHandler(HttpRequestMethodNotSupportedException.class) @ResponseStatus(HttpStatus.METHOD_NOT_ALLOWED) public Result handleHttpRequestMethodNotSupportedException(HttpRequestMethodNotSupportedException e) { log.error(ErrorStatus.METHOD_NOT_ALLOWED.getMessage() + ":" + e.getMessage()); return failure(ErrorStatus.METHOD_NOT_ALLOWED,e); }
@ExceptionHandler(HttpRequestMethodNotSupportedException.class) @ResponseStatus(value = HttpStatus.METHOD_NOT_ALLOWED) public RestResult httpRequestMethodHandler(HttpServletRequest request, HttpRequestMethodNotSupportedException ex) { //ex.printStackTrace(); logger.warn("httpRequestMethodHandler 405 error requestUrl:{}, method:{}, exception:{}", request.getRequestURI(), ex.getMethod(), ex.getMessage()); return RestResultBuilder.builder().errorCode(GlobalErrorCode.METHOD_NOT_ALLOWED).build(); }