1

I have an application which uses quite an old version of spring boot. I have applied interceptor in the application but request is not coming to the interceptor.

@Configuration
public class TestConfiguration extends WebMvcConfigurerAdapter {

@Bean
public GenericInterceptor genericInterceptor() {
    return new GenericInterceptor();
}

@Override
public void addInterceptors(InterceptorRegistry registry) {
    registry.addInterceptor(genericInterceptor()).addPathPatterns("/**");
}}

The bean returned class is GenericInterceptor declared below.

public class GenericInterceptor extends HandlerInterceptorAdapter{
    // custom logic 
}

The main spring boot class is defined below

@SpringBootApplication
@EnableWebMvc
public class TestApplication implements WebApplicationInitializer {

public static void main(String[] args) {
    SpringApplication.run(TestApplication.class, args);
}}

Request is not coming to the Generic Interceptor. Is this because of @EnableWebMvc or something is missing. Can someone explain ?

Aditya
  • 950
  • 8
  • 37
  • Try adding `@EnableWebMvc` for `public class TestConfiguration extends WebMvcConfigurerAdapter` instead of the `TestApplication` – vhthanh Nov 20 '21 at 14:45

1 Answers1

0

You should put @EnableWebMvc for the @Configuration as the document said:

Adding this annotation to an @Configuration class imports the Spring MVC configuration from WebMvcConfigurationSupport

and

Note: only one @Configuration class may have the @EnableWebMvc annotation to import the Spring Web MVC configuration. There can however be multiple @Configuration classes implementing WebMvcConfigurer in order to customize the provided configuration

Try the code below:

@EnableWebMvc
@Configuration
public class TestConfiguration extends WebMvcConfigurerAdapter {

    @Bean
    public GenericInterceptor genericInterceptor() {
        return new GenericInterceptor();
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(genericInterceptor()).addPathPatterns("/**");
    }
}
vhthanh
  • 209
  • 2
  • 6
  • does the spring auto configuration will not provide webmvc support. saw this in this ans: NOTE do not annotate this with @EnableWebMvc, if you want to keep Spring Boots auto configuration for mvc. Can you elaborate on this ?https://stackoverflow.com/questions/31082981/spring-boot-adding-http-request-interceptors – Aditya Nov 20 '21 at 15:52