Interceptor not getting initialized and invoked with SpringBoot
Found the fix. Had to use the ant
like url pattern to match the requests:
registry.addInterceptor(dgvProxySvcRequestInterceptor()).addPathPatterns("/**");
My original configuration was all good; did not require any change except for the above url pattern.
@Configuration
@EnableWebMvc
public class AppConfig extends WebMvcConfigurerAdapter {
// @Bean resolvers , etc
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new DGVProxySvcRequestInterceptor()).addPathPatterns("/controller/action/*");
}
}
I had same issue.
@SpringBootApplication
scans all the component from the current package only. In order to scan other package, we need to specify package in @ComponentScan
.
For Example
package com.main;
@SpringBootApplication
public class Application {
//Code goes here.
}
Now i want interceptor to be register, which is neither in com.main
not com.main.**
, its available in com.test
package.
package com.test.interceptor
@Configuration
public class InterceptorConfig extends WebMvcConfigurerAdapter {
@Autowired
HeaderInterceptor headerInterceptor;
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(headerInterceptor);
}
}
In above case, Spring Boot won't register HeaderInterceptor
in context. In order to register, we must explicitly scan that package.
package com.main;
@SpringBootApplication
@ComponentScan("com.*") //Which takes care all the package which starts with com.
@ComponentScan({"com.main.*","com.test.*"}) //For specific packages
public class Application {
//Code goes here.
}