2016-04-14 100 views
1

我开始使用spring拦截器,并且只想记录每个请求的调用时间。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> 

请求URLlocalhost:8080/mvc/api/getAllItems.rest

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

在我的控制台中,没有sysout输出可用。我试图返回false来确保拦截器和sysout没有问题,但无济于事。

此外,我试图删除删除mvc:annotation-driven标记,但拦截器未被调用。

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

+0

你测试了什么请求url。提供控制器类和web.xml以及应用程序配置 –

回答

0

我发誓,你已经忘记了“MVC:拦截器”:标签:

你能试试以下的“拦截MVC”里面的标签?

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

+0

在发布问题之前,我尝试了相同的方法,但没有奏效。 – ankshah

+0

对不起,再次提问,但您是否尝试在bean拦截器定义中包含mvc:mapping path =“/ **”?另一个问题:你可以尝试只用春天背景做一​​个更简单的测试吗?您同时拥有root-context.xml和servlet-context.xml ....可以尝试只使用一个servlet-context.xml并将bean拦截器移动到那里... –

+0

是的,我试过了,没有工作。另外,当你说servlet-context.xml时,你的意思是web.xml吗?如果是的话,那么根据spring文档,拦截器应该在application context中定义,例如root-context.xml。请参阅[spring docs advisory](http://docs.spring.io/autorepo/docs/spring/3.2.4.RELEASE/javadoc-api/org/springframework/web/servlet/HandlerInterceptor.html) – ankshah

0

要 “覆盖” 我用@Configuration(一个 “配置” 包装内部创建的)注释的配置类拦截。

没有必要使用任何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()); 
    } 

}