java 使用 Spring Web MVC 自定义处理 405 错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26237984/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 09:32:48  来源:igfitidea点击:

Custom handling for 405 error with Spring Web MVC

javaspringspring-mvc

提问by Marceau

In my application, I have a few RequestMappings that only allow POST. If someone happens to fire a GET request at that particular path, they get a 405 error page fed by the container (Tomcat). I can create and define a 405 error page in web.xml for customization.

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

What I want: any request that would result in a 405 error should be handled in a specific controller method.

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

What I've tried:

我试过的:

  • a method with "method = GET" as a counterpart for each of the mappings mentioned. This works fine, but requires me to create an actual requestmapping and method for every path that only allows POST. I find this unnecessary duplication and clutter.
  • a global 'catch' method (requestmapping /*): this does not work, as Spring takes the GET method to be a wrong call to the path specified with POST only
  • an ExceptionHandler-annotated method to handle exceptions of class HttpRequestMethodNotSupportedException: this does not work. It seems that Spring throws and catches this exception entirely in its framework code.
  • specify my own 405 in web.xml. This is not perfect, as I want to have customized handling rather than a static error page.
  • 将“method = GET”作为每个提到的映射对应的方法。这工作正常,但需要我为每个只允许 POST 的路径创建一个实际的请求映射和方法。我发现这种不必要的重复和混乱。
  • 全局“catch”方法(requestmapping /*):这不起作用,因为 Spring 将 GET 方法视为对仅使用 POST 指定的路径的错误调用
  • 一个 ExceptionHandler 注释的方法来处理类 HttpRequestMethodNotSupportedException 的异常:这不起作用。Spring 似乎完全在其框架代码中抛出并捕获此异常。
  • 在 web.xml 中指定我自己的 405。这并不完美,因为我想要自定义处理而不是静态错误页面。

回答by Angad

I would suggest using a Handler Exception Resolver. You can use spring's DefaultHandlerExceptionResolver. Override handleHttpRequestMethodNotSupported()method and return your customized view. This will work across all of your application.

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

The effect is close to what you were expecting in your option 3. The reason your @ExceptionHandlerannotated method never catches your exception is because these ExceptionHandler annotated methods are invoked after a successful Spring controller handler mapping is found. However, your exception is raised before that.

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

回答by Md. Kamruzzaman

Working Code:

工作代码:

@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;
    }
}

In Jsp page (405.jsp):

在 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>