2017-04-27 129 views

回答

1

AsyncRestTemplate扩展了InterceptingAsyncHttpAccessor抽象类,该类揭示了方法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 
    } 
} 

然后使用它:我使用的是不具有这样的性质“setInterceptors”

AsyncRestTemplate asyncRestTemplate = new AsyncRestTemplate(); 
asyncRestTemplate.setInterceptors(Collections.singletonList(new AsyncFooBarInterceptor())); 
+0

AsyncRestTemplate。它来自spring-web-4.0.4.jar –

+0

所以你不使用弹簧? @NIravModi – baao

+0

这是类AsyncRestTemplate的定义,扩展AsyncHttpAccessor实现AsyncRestOperations –