I am trying to build a Spring application and I want to log all requests/responses. I found some examples, but none of them helped. Am trying to create interceptor that will log all informations I need, but interceptor is never called.
Can someone explain why my interceptor in not working?
My web.xml file
<web-app 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_3_0.xsd"
version="3.0">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/business-config.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>mvc-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
This is my config.
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
...
</bean>
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
...enter code here
</bean>
<mvc:annotation-driven/>
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/*"/>
<bean class="ltp.core.security.RequestProcessingInterceptor"/>
</mvc:interceptor>
</mvc:interceptors>
<tx:annotation-driven/>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"/>
<context:component-scan base-package="ltp.core.services.impl"/>
<context:component-scan base-package="ltp.core.security"/>
<context:component-scan base-package="ltp.core.repositories.jpa"/>
<context:component-scan base-package="ltp.core.utils"/>
</beans>
And my interceptor:
public class RequestProcessingInterceptor extends HandlerInterceptorAdapter {
private final Logger logger = Logger.getLogger(RequestProcessingInterceptor.class);
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
logger.info("TRALALALLALAL");
return super.preHandle(request, response, handler);
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
logger.info("[" + LocalDateTime.now().toString() + "] URL: " + request.getRequestURL().toString() + " Send to handler " + handler.toString());
request.setAttribute("startTime", System.currentTimeMillis());
super.postHandle(request, response, handler, modelAndView);
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
logger.info("[" + LocalDateTime.now().toString() + "] Completed in " + (System.currentTimeMillis() - (Long) request.getAttribute("startTime")) + "ms ");
super.afterCompletion(request, response, handler, ex);
}
}
EDIT:
I created mvc-dispatcher-servlet.xml
by moving all mvc related stuff in it. But without <mvc:default-servlet-handler/>
it wasn't working and interceptor is still a problem.
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<mvc:default-servlet-handler/>
<mvc:annotation-driven/>
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/*"/>
<bean class="ltp.core.security.RequestProcessingInterceptor"/>
</mvc:interceptor>
</mvc:interceptors>
</beans>