Spring MVC - Interceptor never called

I am trying to configure an interceptor in my application and I am not being able to make it work.

In my application configuration class, I have configured in the following way:

@Configuration
@EnableWebMvc
public class AppContextConfiguration extends WebMvcConfigurerAdapter {
    ...
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new MyInterceptor());
    }
    ...
}

And the interceptor:

public class MyInterceptor extends HandlerInterceptorAdapter{

    private static final Logger logger = LoggerFactory.getLogger(MyInterceptor.class);

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, 
        Object handler) throws Exception {

        logger.debug("MyInterceptor - PREHANDLE");
    }
}

Does anybody know why is not being invoked?


I'm using Spring Boot and was having the same problem where addInterceptors() was being called to register the interceptor, but the interceptor never fired during a request. Yet XML configuration worked no problem.

Basically, you don't need the WebMvcConfigurerAdapter class. You just need to declare an @Bean of type MappedInterceptor:

@Bean
public MappedInterceptor myInterceptor()
{
    return new MappedInterceptor(null, new MyInterceptor());
}

Interceptor classes must be declared in spring context xml configuration file within the tag <mvc:interceptors>. Did you do that?

From the Documentation

An example of registering an interceptor applied to all URL paths:

<mvc:interceptors>
    <bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor" />
</mvc:interceptors>

An example of registering an interceptor limited to a specific URL path:

<mvc:interceptors>
    <mvc:interceptor>
        <mapping path="/secure/*"/>
        <bean class="org.example.SecurityInterceptor" />
    </mvc:interceptor>
</mvc:interceptors>

So, you would need to configure MyInterceptor class in the spring context xml file


Can someone please mark Theos answer as the correct one? I had the situation of a perfectly working Spring Boot app using i18n and Thymeleaf (with a layout interceptor) as long as the app was running localhost with the following config:

@Override
public void addInterceptors(InterceptorRegistry registry) {
    registry.addInterceptor(localeChangeInterceptor());
    registry.addInterceptor(thymeleafLayoutInterceptor());
}

As soon as I deployed the app to an Elasticbeanstalk instance, both interceptors were not fired anymore. Although added once. When I changed the setting to

@Bean
public MappedInterceptor localeInterceptor() {
    return new MappedInterceptor(null, localeChangeInterceptor());
}

@Bean
public MappedInterceptor thymeleafInterceptor() {
    return new MappedInterceptor(null, thymeleafLayoutInterceptor());
}

everything was working fine on all environments. There must be an issue with firing interceptors added with addInterceptor, it might depend on the URL that is used to invoke the request - I don't know.

Thanks for your answer, Theo, I just wanted to add this here if some else stumbles upon this nice feature.