Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring HandlerInterceptor called twice

In a library project (using Spring 3.2.4) I define multiple interceptors. The servlet config xml files are included in the jar to be imported in the web application. The interceptors are used in multiple servlet xmls, because they shall be used for different Dispatcher servlets with different interceptors.

The problem is, that the Interceptors are called twice, but the handler(controller) is just called once.

The interceptor is defined in the library project:

public class SomeInterceptor extends HandlerInterceptorAdapter {

        @Override
        public void afterCompletion(final HttpServletRequest request, final HttpServletResponse response, final Object handler, final Exception ex) throws Exception {
            System.out.println("afterCompletionCalled");
        }

        @Override
        public boolean preHandle(final HttpServletRequest request, final HttpServletResponse response, final Object handler) throws Exception {
            System.out.println("preHandle called");
            return true;
        }

        @Override
        public void postHandle(final HttpServletRequest request, final HttpServletResponse response, final Object handler, final ModelAndView modelAndView) throws Exception {
        }
    }

The servlet configuration is provided in a jar file and later on included in the application.

libservletcfg.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:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
       http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd">

    <context:component-scan base-package="com.example.controller" annotation-config="true" />

    <bean id="jacksonMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"/>

    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
        <property name="messageConverters">
            <list>
                <ref bean="jacksonMessageConverter"/>
            </list>
        </property>
    </bean>

    <mvc:annotation-driven />

   <mvc:interceptors>
        <mvc:interceptor>
            <mvc:mapping path="/**"/>
            <bean class="com.example.SomeInterceptor"/>
        </mvc:interceptor>    
    </mvc:interceptors>

</beans>

In the web application I just include the libservletcfg.xml in the servlet config.

servletConfig.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:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">

    <import resource="classpath*:libservletcfg.xml"/>

    <context:component-scan base-package="com.example.app.controller" annotation-config="true" />

</beans>

This servletConfig.xml is used as context config for the Dispatcher servlet in the web.xml:

<servlet>
    <servlet-name>someServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath*:servletConfig.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>someServlet</servlet-name>
    <url-pattern>/someUrl/*</url-pattern>
</servlet-mapping>

Can anyone tell my why the interceptor is called twice or any problems with the configuration (which might lead to an unexpected behaviour)?

edit

Example of a Controller called twice:

@Controller
@RequestMapping(value = "/someUrl")
public class SampleController {

    @RequestMapping(method = RequestMethod.POST)
    public @ResponseBody SampleResponseBody sampleMethod(@RequestBody final SampleRequestBody pSampleRequestBody) {
        final SampleResponseBody response = new SampleResponseBody();
        return response;
    }

}
like image 716
Daniel Vermaasen Avatar asked Jun 27 '26 18:06

Daniel Vermaasen


2 Answers

Check you don't have another libservletcfg.xml hanging around. classpath*: will match all of them.

like image 144
Osvaldo Avatar answered Jun 29 '26 07:06

Osvaldo


I was also facing same issue, the interceptor was getting called twice. The issue was in @ComponentScan declaration of root config class, two instances were getting created, one in a web context and another in the root context.

So, after removing the interceptor package from the root context issue got fixed.

like image 37
Amit Patil Avatar answered Jun 29 '26 07:06

Amit Patil



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!