@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; }
@Test(expected = HttpRequestMethodNotSupportedException.class) public void call_to_get_always_throws_super_method() throws Exception { UaaTokenEndpoint endpoint = new UaaTokenEndpoint(); endpoint.setAllowedRequestMethods(allowedRequestMethods); endpoint.setAllowQueryString(false); try { endpoint.getAccessToken(mock(Principal.class), emptyMap()); } catch (HttpRequestMethodNotSupportedException e) { assertEquals("GET", e.getMethod()); throw e; } }
@Test(expected = HttpRequestMethodNotSupportedException.class) public void call_to_get_always_throws_override_method() throws Exception { UaaTokenEndpoint endpoint = new UaaTokenEndpoint(); endpoint.setAllowedRequestMethods(allowedRequestMethods); endpoint.setAllowQueryString(false); try { endpoint.doDelegateGet(mock(Principal.class), emptyMap()); } catch (HttpRequestMethodNotSupportedException e) { assertEquals("GET", e.getMethod()); throw e; } } }
/** * 通过HttpRequestMethodNotSupportedException初始化 * * @param cause * HttpRequestMethodNotSupportedException */ public HttpRequestMethodNotSupportedExceptionInfo(HttpRequestMethodNotSupportedException cause) { super(cause, 405); method = cause.getMethod(); }
/** * 通过HttpRequestMethodNotSupportedException初始化 * * @param cause * HttpRequestMethodNotSupportedException */ public HttpRequestMethodNotSupportedExceptionInfo(HttpRequestMethodNotSupportedException cause) { super(cause, 405); method = cause.getMethod(); }
@Test public void getRequestNotAllowed() throws Exception { MockHttpServletRequest request = new MockHttpServletRequest(); request.setMethod("GET"); request.setParameter("foo", "bar"); request.setRequestURI("/postOnly"); try { this.integrationRequestMappingHandlerMapping.getHandler(request); } catch (HttpRequestMethodNotSupportedException e) { assertEquals("GET", e.getMethod()); assertArrayEquals(new String[] {"POST"}, e.getSupportedMethods()); } }
@Override protected ResponseEntity<Object> handleHttpRequestMethodNotSupported(final HttpRequestMethodNotSupportedException ex, final HttpHeaders headers, final HttpStatus status, final WebRequest request) { logger.info(ex.getClass().getName()); // final StringBuilder builder = new StringBuilder(); builder.append(ex.getMethod()); builder.append(" method is not supported for this request. Supported methods are "); ex.getSupportedHttpMethods().forEach(t -> builder.append(t + " ")); final ApiError apiError = new ApiError(HttpStatus.METHOD_NOT_ALLOWED, ex.getLocalizedMessage(), builder.toString()); return new ResponseEntity<Object>(apiError, new HttpHeaders(), apiError.getStatus()); }
/** * 请求方式不支持 */ @ExceptionHandler({HttpRequestMethodNotSupportedException.class}) public AjaxResult handleException(HttpRequestMethodNotSupportedException e) { log.error(e.getMessage(), e); return AjaxResult.error("不支持' " + e.getMethod() + "'请求"); }
@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(); }
return ApiResponse.ofStatus(Status.REQUEST_NOT_FOUND); } else if (e instanceof HttpRequestMethodNotSupportedException) { log.error("【全局异常拦截】HttpRequestMethodNotSupportedException: 当前请求方式 {}, 支持请求方式 {}", ((HttpRequestMethodNotSupportedException) e).getMethod(), JSONUtil.toJsonStr(((HttpRequestMethodNotSupportedException) e).getSupportedHttpMethods())); return ApiResponse.ofStatus(Status.HTTP_BAD_METHOD); } else if (e instanceof MethodArgumentNotValidException) {
String method = ((HttpRequestMethodNotSupportedException) exception).getMethod();