ElderMael
ElderMael

Reputation: 7101

Spring MVC Interceptors stop working when mixing annotations

I have a web app in which we stop supporting IE 7. To notify users of this change, we had a Spring MVC HandlerInterceptorAdapter in charge of redirect the user to a warning url.

This is the relevant deployment descriptor (web.xml) content:

<servlet>
    <servlet-name>springServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            /WEB-INF/web-servlet.xml
        </param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

This application only used old school Spring MVC controllers i.e. no annotations; recently I had to configure a controller using annotations using the following XML:

<beans>
    <!-- ... -->
    <!-- A lot of beans and controllers using Classic Spring MVC -->

    <bean id="browserVersionInterceptor" class="...BrowserVersionInterceptor" />

    <bean id="handlerMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
        <property name="detectHandlersInAncestorContexts" value="true" />
        <property name="interceptors">
            <list>
                <ref bean="browserVersionInterceptor" />
            </list>
        </property>
    </bean>
    <beans>
        <context:annotation-config />      
        <bean id="mvcAnnotationConfig"
        class="....MvcAnnotationConfig">
        </bean>
    </beans>
</beans>

And in that configuration I set the component scan for the controllers:

@Configuration
@EnableWebMvc
@ComponentScan("package.to.controllers")
public class MvcAnnotationConfig {
    //...not much here
}

After applying this change, I found that no interceptors declared in the previous section of the file worked.

And the newly created controller with annotations:

@Controller
@RequestMapping("/Tickets.html")
public class SupportProcessController {

    @RequestMapping(method = GET)
    public ModelAndView show() {...}

    @ResponseBody
    @RequestMapping(method = POST, produces = "application/json")
    public Object apply() {...}
}

Any ideas why this happened?

Upvotes: 0

Views: 748

Answers (1)

Serge Ballesta
Serge Ballesta

Reputation: 148880

If your controllers are declared using a @ComponentScan annotation, you could try to use a <mvc:interceptors> block in your xml config. Something like :

<mvc:interceptors>
    <ref bean="browserVersionInterceptor"/>
</mvc:interceptors>

But read the doc too ...

Upvotes: 1

Related Questions