Is it possible to write AOP for Spring RestTemplate class using spring AOP or Aspectj. EX:
@Around("execution(* org.springframework.web.client.RestTemplate.getFor*(..))")
Thanks
Is it possible to write AOP for Spring RestTemplate class using spring AOP or Aspectj. EX:
@Around("execution(* org.springframework.web.client.RestTemplate.getFor*(..))")
Thanks
I had the same issue and couldn't make it work with AOP.
However, in this case, there is a workaround. Since RestTemplate
extends InterceptingHttpAccessor
, you can intercept all requests coming through theRestTemplate
object.
Sample configuration that logs all HTTP requests :
@Bean
public RestTemplate restTemplate() {
RestTemplate restTemplate = new RestTemplate();
// (...)
// setup code for the RestTemplate object
restTemplate.getInterceptors().add((request, body, execution) -> {
logger.info("HTTP {} request to {}", request.getMethod(), request.getURI());
return execution.execute(request, body);
});
return restTemplate;
}
While this is not equivalent to using an aspect, you can get similar functionality with interceptors and pretty minimal configuration.