ankshah
ankshah

Reputation: 435

Spring interceptor is not getting invoked

I am getting started with spring interceptors and wanted to simply log the time of invocation of each request.

My Interceptor class:

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>

Controller Class

    @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");
            }
         ....
     }  

I have no request mapping for the controller class, instead I am mapping requests directly to methods. (I know that is a bad practice, but I am working on it :) )

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>

Request URL localhost:8080/mvc/api/getAllItems.rest

As per my understanding since no mapping is provided in the mvc:interceptors tag, the interceptor should be invoked for all requests.

In my console, no sysout outputs are available. I tried returning false to ensure that there is no issue with interceptors and sysout but no avail.

Also, I tried removing removing mvc:annotation-driven tag, but the interceptor is not invoked.

Any pointers what might be wrong here? There were other options like creating bean of class BeanNameUrlHandlerMapping and creating bean of class RequestMappingHandlerMapping with properties:list and mapping, but none of them work. Can someone point out the difference between these methods and give directions when to use which?

Upvotes: 1

Views: 2566

Answers (2)

Dror
Dror

Reputation: 5495

To "override" the interceptor I used the configuration class annotated with @Configuration (created inside a "config" package).

There's no need to use any xml configuration.

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());
    }

}

Upvotes: 1

Pablo Ezequiel Inchausti
Pablo Ezequiel Inchausti

Reputation: 20513

I would swear that you have forgotten the "mvc:interceptor" tag inside the "mvc:interceptors" tag:

Could you try the following?

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

Upvotes: 0

Related Questions