本文介绍了Spring MVC处理程序拦截器未运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下拦截器类:

package cz.coffeeexperts.feedback.server.web.interceptors;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

public class RestAuthorizationInterceptor extends HandlerInterceptorAdapter {

    @Override
    public boolean preHandle(HttpServletRequest request,
            HttpServletResponse response, Object handler)
        throws Exception {

        System.out.println("fuu");
        response.setStatus( HttpServletResponse.SC_UNAUTHORIZED );
        return false;
    }
}

我在spring-webmvc.xml中对其进行了如下配置:

I configured it inside my spring-webmvc.xml as following :

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd

       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">

    <mvc:annotation-driven/>

    <mvc:interceptors>
     <mvc:interceptor>
       <mvc:mapping path="/rest/api/01/status" />
       <bean class="cz.coffeeexperts.feedback.server.web.interceptors.RestAuthorizationInterceptor" />
     </mvc:interceptor>
    </mvc:interceptors>

</beans>

但是,当我转到http://localhost:8080/myserver/rest/api/01/status时,会得到状态码为200的常规答案(与添加拦截器之前相同).另外,不会打印消息"fuu"(因此不会调用preHandle方法).

However when I go to http://localhost:8080/myserver/rest/api/01/status, I get regular answer with status code 200 (same as before I added interceptor). Also, message "fuu" is not printed (so the preHandle method is not called).

有什么想法吗?我从以下示例开始进行操作: http://javapapers.com/spring/spring -mvc-handler-interceptor/,但是所有其他示例看起来都一样,我找不到哪里出了错.

Any ideas? I started to do it with this example : http://javapapers.com/spring/spring-mvc-handler-interceptor/, but all other examples look the same, I cant find where I go wrong.

我正在使用Spring 3.2.4.RELEASE

I am using Spring 3.2.4.RELEASE

重要的修改,可与此配合使用:

Important edit, it works with this :

<mvc:interceptors>
 <mvc:interceptor>
   <mvc:mapping path="/**" />
   <bean class="cz.coffeeexperts.feedback.server.web.interceptors.RestAuthorizationInterceptor" />
 </mvc:interceptor>
</mvc:interceptors>

所以问题是,我的路径出了什么问题?

So the question is, what is wrong with my path?

推荐答案

好,我找到了解决方案,因为我的路径是用以下命令定义的:

Ok, I found solution, because my path is defined with following :

<servlet-mapping>
    <servlet-name>rest</servlet-name>
    <url-pattern>/rest/*</url-pattern>
</servlet-mapping>

这就是我的控制器的外观

And this is how my controller looks like

@Controller
@RequestMapping(value = "/api")
public class ApiController {
 @RequestMapping(value = "/01/status", method = RequestMethod.GET)
    @ResponseBody
    public ServerStatusJSON getStatus(HttpServletResponse response) {
        ...
    }
}

此地址http://localhost:8080/myserver/rest/api/01/status的工作配置如下:

The working configuration for this address : http://localhost:8080/myserver/rest/api/01/status is as following :

<mvc:interceptors>
 <mvc:interceptor>
   <mvc:mapping path="/api/01/status" />
   <bean class="cz.coffeeexperts.feedback.server.web.interceptors.RestAuthorizationInterceptor" />
 </mvc:interceptor>
</mvc:interceptors>

PS:感谢geoand,他把我推向正确的方向.

PS : My thanks to geoand, he pushed me to the right way.

这篇关于Spring MVC处理程序拦截器未运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 20:24