Search code examples
javaspringspring-mvcspring-web

Spring WebMVC 5 - Annotation based Interceptor


How to configure an Interceptor, through Annotation only(I do NOT like to register the interceptor in .XML file, I do not use .XML based configuration)?

Note : I see in example on internet it is suggesting to use org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter, when I tried to use it, I found it is DEPRECATED

I am testing on SpringWebMVC-5 with SpringBoot-2


Solution

  • In Spring5 you can use org.springframework.web.servlet.config.annotation.WebMvcConfigurer:

    @EnableWebMvc
    @Configuration
    public class WebConfig implements WebMvcConfigurer {
    
         @Override
         public void addInterceptors(InterceptorRegistry registry) {
              registry.addInterceptor(....);
         }
     }