是否可以使用spring AOP或Aspectj为Spring RestTemplate类编写AOP。例如:
@Around("execution(* org.springframework.web.client.RestTemplate.getFor*(..))")
谢谢
最佳答案
我遇到了同样的问题,但无法使其与 AOP 一起工作。
但是,在这种情况下,有一个解决方法。由于 RestTemplate
扩展了 InterceptingHttpAccessor
,因此您可以拦截通过 RestTemplate
对象传入的所有请求。
记录所有 HTTP 请求的示例配置:
@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;
}
虽然这并不等同于使用切面,但您可以通过拦截器和相当少的配置获得类似的功能。
关于spring - 是否可以使用 Spring AOP 或 Aspectj 为 Spring RestTemplate 类和任何外部 jar 类编写 AOP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40428175/