为什么spring中的AsyncRestTemplate不';t有RestTemplate这样的拦截器吗?

为什么spring中的AsyncRestTemplate不';t有RestTemplate这样的拦截器吗?,spring,spring-web,Spring,Spring Web,我有一种情况,我需要截获请求,我需要在该请求中设置授权头。 所以我得到了一个解决方案,我可以使用拦截器来设置这个头,但是当我选中AsyncRestTemplate时,它就没有RestTemplate那样的属性了 是否有任何不包含该属性的具体原因?AsyncRestTemplate扩展了InterceptingAsynchtAccessor抽象类,该类公开了方法setInterceptors。当然,您可以设置拦截器,就像使用非异步RestTemplate一样。请注意,拦截器需要实现AsyncCli

我有一种情况,我需要截获请求,我需要在该请求中设置授权头。 所以我得到了一个解决方案,我可以使用拦截器来设置这个头,但是当我选中AsyncRestTemplate时,它就没有RestTemplate那样的属性了


是否有任何不包含该属性的具体原因?

AsyncRestTemplate扩展了
InterceptingAsynchtAccessor
抽象类,该类公开了方法
setInterceptors
。当然,您可以设置拦截器,就像使用非异步RestTemplate一样。请注意,拦截器需要实现
AsyncClientHttpRequestInterceptor

public class AsyncFooBarInterceptor implements AsyncClientHttpRequestInterceptor {

    @Override
    public ListenableFuture<ClientHttpResponse> intercept(HttpRequest request, byte[] body, AsyncClientHttpRequestExecution execution) throws IOException {
        return null; // do your thing instead
    }
}

我使用的AsyncRestTemplate没有属性“setInterceptors”。它来自spring-web-4.0.4.jar,所以你没有使用spring@NiravModi这是类AsyncRestTemplate extends AsyncHttpAccessor的定义,它实现AsyncRestOperations以使
AsyncRestTemplate
extend
InterceptingAsyncHttpAccessor
在4.2.4和4.3.5之间的某个时间生成。
AsyncRestTemplate asyncRestTemplate = new AsyncRestTemplate();
asyncRestTemplate.setInterceptors(Collections.singletonList(new AsyncFooBarInterceptor()));