java – Spring MVC Handler Interceptor不运行

我有以下拦截器类:

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中配置它如下:

<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方法).

有任何想法吗?我开始用这个例子做到这一点:http://javapapers.com/spring/spring-mvc-handler-interceptor/,但所有其他例子看起来都一样,我找不到出错的地方.

我使用的是Spring 3.2.4.RELEASE

重要的编辑,它适用于此:

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

所以问题是,我的道路有什么问题?

解决方法:

好的,我找到了解决方案,因为我的路径定义如下:

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

这就是我的控制器的样子

@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如下:

<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,他把我推向正确的方向.

上一篇:如何在HandlerInterceptorAdapter中使用@ResponseBody记录Spring 3控制器的JSON响应?


下一篇:spring – 为什么我的自定义ClientHttpRequestInterceptor具有空响应