使用HandlerInterceptorAdapter类仅拦截GET请求

时间:2017-08-17 09:55:46

标签: spring-mvc spring-boot interceptor

是否可以在Spring启动应用程序中仅拦截GET请求?

addPathPatterns()方法无法解决我的目的。

我想排除或包含基于其请求方法截获的请求。

例如:

  1. 仅拦截GET请求
  2. 仅拦截GET和POST请求。
  3. 应用程序将接收GET,POST,PUT和DELETE请求。

1 个答案:

答案 0 :(得分:0)

实际上可以。 您将拦截所有方法,但必须从请求中使用getMethod()对其进行过滤。这是它的javadoc:

 /**
     * Returns the name of the HTTP method with which this 
     * request was made, for example, GET, POST, or PUT.
     * Same as the value of the CGI variable REQUEST_METHOD.
     *
     * @return          a <code>String</code> 
     *              specifying the name
     *              of the method with which
     *              this request was made
     */
    public String getMethod();
相关问题