是否有可能使用Spring AOP或Aspectj为Spring RestTemplate类和任何外部jar类编写AOP



是否可以使用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;
}
虽然这并不等同于使用方面,但您可以使用拦截器和非常少的配置来获得类似的功能。

最新更新