Spring拦截器没有被调用

时间:2016-04-14 16:57:28

标签: java spring spring-mvc interceptor

我开始使用spring拦截器,并希望只记录每个请求的调用时间。

我的拦截器类:

package com.ankit.notice.interceptor;

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

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

public class ExecuteTimeInterceptor extends HandlerInterceptorAdapter{

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

        long startTime = System.currentTimeMillis();
        System.out.println("Time is " + startTime);
        return true;
    }
}

根context.xml中

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
    http://www.springframework.org/schema/context                http://www.springframework.org/schema/context/spring-context-3.0.xsd  
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">

<!-- Root Context: defines shared resources visible to all other web components -->

<context:component-scan base-package="com.ankit.notice" />
<mvc:annotation-driven></mvc:annotation-driven>
<mvc:interceptors>
    <bean class="com.ankit.notice.interceptor.ExecuteTimeInterceptor" />
</mvc:interceptors> 

   .....
</beans>

控制器类

    @RequestMapping(value = "api/getAllItems.rest", method =     RequestMethod.GET)  
    public ResponseEntity<Map<String, Object>> getAllItems(
        HttpServletRequest request, HttpServletResponse response, HttpSession session) {
        ResponseEntity<Map<String, Object>> responseEntity = null;
        Map<String, Object> responseParams = new HashMap<String, Object>();

        try {
            User user = (User) session.getAttribute("user");
            if ((null == user) || (user.getId() < 0)) {
                throw new MNPSessionException(
                        "INVALID USER : Session attribute user is not set correctly");
            }
         ....
     }  

我没有控制器类的请求映射,而是将请求直接映射到方法。 (我知道这是一个不好的做法,但我正在努力:))

web.xml

        <?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee  


http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

<!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/spring/root-context.xml</param-value>
</context-param>

<context-param>
    <param-name>defaultHtmlEscape</param-name>
    <param-value>true</param-value>
</context-param>

<!-- Creates the Spring Container shared by all Servlets and Filters -->
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<!-- Processes application requests -->
<servlet>
    <servlet-name>appServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>appServlet</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>
<servlet-mapping>
    <servlet-name>appServlet</servlet-name>
    <url-pattern>*.htm</url-pattern>
</servlet-mapping>  
<servlet-mapping>
    <servlet-name>appServlet</servlet-name>
    <url-pattern>*.json</url-pattern>
</servlet-mapping>
    <servlet-mapping>
    <servlet-name>appServlet</servlet-name>
    <url-pattern>*.rest</url-pattern>
</servlet-mapping>  

 <filter>
    <filter-name>sessionCheckFilter</filter-name>
    <filter-class>com.ankit.notice.util.SessionCheckFilter</filter-class>
</filter>  

filter-mapping>
<filter-name>sessionCheckFilter</filter-name>
<url-pattern>*</url-pattern>
</filter-mapping>  
</web-app>

请求网址 localhost:8080/mvc/api/getAllItems.rest

根据我的理解,因为mvc:interceptors标记中没有提供映射,所以应该为所有请求调用拦截器。

在我的控制台中,没有可用的sysout输出。我尝试返回false以确保拦截器和sysout没有问题,但没有用。

另外,我尝试删除删除mvc:annotation-driven标记,但不调用拦截器。

任何指针在这里可能有什么问题?还有其他选项,比如创建类BeanNameUrlHandlerMapping的bean和创建类RequestMappingHandlerMapping的bean,其属性为listmapping,但它们都不起作用。有人可以指出这些方法之间的区别,并指出何时使用哪种方法?

2 个答案:

答案 0 :(得分:0)

我发誓你已经忘记了&#34; mvc:拦截器&#34;标签内的&#34; mvc:拦截器&#34;标记:

您可以尝试以下方法吗?

&#13;
&#13;
<mvc:interceptors>
  
    <mvc:interceptor>
        <mvc:mapping path="/**" />
        <bean  id="executeTimeInterceptor"  class="com.ankit.notice.interceptor.ExecuteTimeInterceptor" />
    </mvc:interceptor>
  
</mvc:interceptors>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

To&#34;覆盖&#34;拦截器我使用配置类注释 @Configuration (在&#34; config&#34;包中创建)。

无需使用任何xml配置。

package com.abc.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import com.abc.interceptor.BaseInterceptor;

@Configuration
public class WebConfig extends WebMvcConfigurerAdapter {

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new BaseInterceptor());
    }

}