I'm currently trying to incorporate a HandlerInterceptorAdapter but it's not getting registered and comparing it to other answers is tough because everyone is using something different. And I'm aware WebMvcConfigureAdapter is deprecated, some versioning is beyond my control for the scope of the project, see usage specs below.
Can someone please provide some guidance on incorporating interceptors with a RestTemplate (that's not ClientHttpRequestInterceptor).
Main:
@SpringBootApplication @EnableRetry public class Application extends SpringBootServletInitializer { public static void main(String[] args) { ApplicationContext ctx = SpringApplication.run(Application.class, args); } @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder applicationBuilder) { return applicationBuilder.sources(Application.class); } @Bean private RestTemplate restTemplate(){ Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("redacted", 8080)); SimpleClientHttpRequestFactory simpleClientHttpRequestFactory = new SimpleClientHttpRequestFactory(); simpleClientHttpRequestFactory.setProxy(proxy); simpleClientHttpRequestFactory.setOutputStreaming(false); RestTemplate template = new RestTemplate(); template.setErrorHandler(new MyResponseErrorHandler()); return template; } } Interceptor : com.example.foo.config.request.interceptor
@Component public class MyInterceptor extends HandlerInterceptorAdapter { @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { System.out.println("INTERCEPTED"); return super.preHandle(request, response, handler); } } InterceptorConfig : com.example.foo.config.request.interceptor
@Configuration public class InterceptorConfig extends WebMvcConfigurerAdapter { @Bean MyInterceptor myInterceptor() { return new MyInterceptor(); } @Override public void addInterceptors(InterceptorRegistry registry) { super.addInterceptors(registry); System.out.println("Adding interceptor"); registry.addInterceptor(myInterceptor()); } } "Adding interceptor" does get logged so I know the configs are being scanned. I just can't get any interceptor logic to log.
Using:
- Spring Boot v1.5.15
- Spring Version: 4.3.18.RELEASE
RestTemplate. Please use it with autowired@Autowired private RestTemplate restTemplate;. Even the interceptor should beAutowired. I would suggest see this project as an example github.com/Asatnin/SpringMicroservices/blob/…