【发布时间】:2016-04-14 16:57:27
【问题描述】:
我开始使用 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;
}
}
root-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 和使用属性:list 和 mapping 创建 RequestMappingHandlerMapping 类的 bean,但它们都不起作用。有人能指出这些方法之间的区别,并指出何时使用哪种方法吗?
【问题讨论】:
-
你在测试什么请求 url。提供控制器类和web.xml以及应用配置
标签: java spring spring-mvc interceptor