2017-04-27 130 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 –