3

I'm trying to convert a Spring project from XML to Java config and have run into the following issue with HandlerInterceptors:

XML Config (works):

<mvc:annotation-driven />
<mvc:interceptors>
    <bean class="com.mycompany.MyHandlerInterceptor" />
</mvc:interceptors>

Java Config (interceptor is never called)

@Configuration
public class MvcConfig extends WebMvcConfigurationSupport {

    @Override
    protected void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new MyHandlerInterceptor());
    }

    // ...
}

According to the documentation, these two configurations should be equivalent, however in the Java config example the neither the pre or post handle methods are ever called?

What am I missing?

Thanks.

3
  • Works fine here. Have you used your debugger or added traces in the code to make sure that your MvcConfig class is loaded and the addInterceptors method called? Commented Feb 15, 2014 at 15:42
  • @JBNizet Yes. I added debug logging before/after the call to registry.addInterceptor() and they both were hit. Commented Feb 15, 2014 at 16:23
  • Try to add @EnableWebMvc to MvcConfig class. See stackoverflow.com/questions/10391988/… Commented Feb 15, 2014 at 17:08

1 Answer 1

6

This was my own fault. I had overridden requestMappingHandlerMapping() in my MVC Java config and did not set the interceptors property on the custom HandlerMapping class.

@Bean
@Override
public RequestMappingHandlerMapping requestMappingHandlerMapping() {
    CustomRequestHandlerMapping handlerMapping = new CustomRequestHandlerMapping();
    handlerMapping.setOrder(0);
    handlerMapping.setInterceptors(getInterceptors()); // <-- This was missing
    return handlerMapping;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.