【问题标题】:Custom handling for 405 error with Spring Web MVC使用 Spring Web MVC 自定义处理 405 错误
【发布时间】:2014-12-01 23:55:26
【问题描述】:

在我的应用程序中,我有一些只允许 POST 的 RequestMappings。如果有人碰巧在该特定路径上触发 GET 请求,他们会收到由容器 (Tomcat) 提供的 405 错误页面。我可以在 web.xml 中创建和定义一个 405 错误页面进行自定义。

我想要的:任何会导致 405 错误的请求都应该在特定的控制器方法中处理。

我尝试过的:

  • 一种方法,其中“method = GET”作为提到的每个映射的对应项。这很好用,但需要我为每个只允许 POST 的路径创建一个实际的请求映射和方法。我发现这是不必要的重复和混乱。
  • 全局“catch”方法(requestmapping /*):这不起作用,因为 Spring 将 GET 方法视为对仅使用 POST 指定的路径的错误调用
  • 一个 ExceptionHandler 注释的方法来处理类 HttpRequestMethodNotSupportedException 的异常:这不起作用。 Spring 似乎完全在其框架代码中抛出并捕获了这个异常。
  • 在 web.xml 中指定我自己的 405。这并不完美,因为我想要自定义处理而不是静态错误页面。

【问题讨论】:

    标签: java spring spring-mvc


    【解决方案1】:
    You can use spring DefaultHandlerExceptionResolver. 
    Override handleHttpRequestMethodNotSupported() method and return your customized.
    Status Code: 405
    
    
    
    
    protected ResponseEntity<Object> handleHttpRequestMethodNotSupported(HttpRequestMethodNotSupportedException ex, HttpHeaders headers, HttpStatus status, WebRequest request) {
                ApiError apiError = ApiError.builder()
                        .status(HttpStatus.METHOD_NOT_ALLOWED)
                        .message(ex.getMessage())
                        .build();
        
                return new ResponseEntity<Object>(apiError, new HttpHeaders(), apiError.getStatus());
            }
    

    响应结果:

    {
        "status": "METHOD_NOT_ALLOWED",
        "message": "Request method 'POST' not supported",
        "errors": null
    }
    

    【讨论】:

      【解决方案2】:

      工作代码:

      @ControllerAdvice
      public class GlobalExceptionController {
      
          @ExceptionHandler(HttpRequestMethodNotSupportedException.class)
          public ModelAndView handleError405(HttpServletRequest request, Exception e) {
              ModelAndView mav = new ModelAndView("/405");
              mav.addObject("exception", e);  
              //mav.addObject("errorcode", "405");
              return mav;
          }
      }
      

      在Jsp页面中(405.jsp):

      <div class="http-error-container">
          <h1>HTTP Status 405 - Request Method not Support</h1>
          <p class="message-text">The request method does not support. <a href="<c:url value="/"/>">home page</a>.</p>
      </div>
      

      【讨论】:

      • 完美,这个答案教会了我一个新东西,我现在可以处理异常了
      【解决方案3】:

      我建议使用处理程序异常解析器。你可以使用spring的DefaultHandlerExceptionResolver。覆盖handleHttpRequestMethodNotSupported() 方法并返回您自定义的view。这将适用于您的所有应用程序。

      效果接近您在选项 3 中所期望的效果。您的 @ExceptionHandler 注释方法永远不会捕获您的异常的原因是因为这些 ExceptionHandler 注释方法是在找到成功的 Spring 控制器处理程序映射后调用的。但是,在此之前引发了您的异常。

      【讨论】:

      • 这太完美了!其他人可能需要注意的一件事是,如果您决定这样做并覆盖 DefaultHandlerExceptionResolver,您还应该覆盖 Ordered 接口中指定的 getOrder() 方法。在该方法中,您应该返回任何低于 Integer.MAX_VALUE 的 int。原因是最低顺序的处理程序首先获得了异常处理。无论如何,谢谢!
      猜你喜欢
      • 2011-12-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-13
      • 2010-12-24
      • 2010-09-27
      • 1970-01-01
      相关资源
      最近更新 更多