1
我有一種情況需要攔截請求,我需要將授權頭設置爲該請求。 所以我得到的解決方案,我可以使用攔截器來設置標題,但當我檢查AsyncRestTemplate然後它沒有像RestTemplate那樣的屬性。爲什麼AsyncRestTemplate在彈簧中沒有像RestTemplate這樣的攔截器?
是否有任何特定原因不包括該屬性?
我有一種情況需要攔截請求,我需要將授權頭設置爲該請求。 所以我得到的解決方案,我可以使用攔截器來設置標題,但當我檢查AsyncRestTemplate然後它沒有像RestTemplate那樣的屬性。爲什麼AsyncRestTemplate在彈簧中沒有像RestTemplate這樣的攔截器?
是否有任何特定原因不包括該屬性?
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()));
AsyncRestTemplate。它來自spring-web-4.0.4.jar –
所以你不使用彈簧? @NIravModi – baao
這是類AsyncRestTemplate的定義,擴展AsyncHttpAccessor實現AsyncRestOperations –