如何将标题添加到ODataClient(Apache Olingo)?

时间:2018-12-27 19:37:11

标签: java olingo

我在访问odata服务时遇到麻烦,因为它需要特定的标头,而且我无法直接在ODataClient上添加它,有人对如何解决我的问题有任何想法吗?

伪代码:

ODataClient client = ODataClientFactory.getClient(); 
client.addHeader("Header","123456789"); // <---- this code is what I am seeking
URI customersUri = client.newURIBuilder("uri/northwindmodel.svc")
          .appendEntitySetSegment("Customers").build();
ODataRetrieveResponse<ODataEntitySetIterator<ODataEntitySet, ODataEntity>> response
 = client.getRetrieveRequestFactory().getEntitySetIteratorRequest(customersUri).execute();

依赖性:

<dependency>
    <groupId>org.apache.olingo</groupId>
    <artifactId>odata-client-core</artifactId>
    <version>4.5.0</version>
</dependency>

1 个答案:

答案 0 :(得分:1)

ODataClient没有API来设置其他标头,可能是因为它直接不直接拥有HttpClient。它拥有我的AbstractODataRequest类,因此您有API可以为每个HTTP请求设置附加标头。

您可以在addCustomHeader中使用ODataRequest方法。

例如您可以通过以下方式重构示例代码以实现目标。

ODataClient client = ODataClientFactory.getClient(); 
// client.addHeader("Header","123456789"); // <---- this code is what I am seeking
URI customersUri = client.newURIBuilder("uri/northwindmodel.svc")
        .appendEntitySetSegment("Customers").build();

ODataEntitySetIteratorRequest<ClientEntitySet, ClientEntity> entitySetIteratorRequest =
        client.getRetrieveRequestFactory().getEntitySetIteratorRequest(qryUri);

entitySetIteratorRequest.addCustomHeader("Custom-Header-key", "Custom-Header-Value");

ODataRetrieveResponse<ClientEntitySetIterator<ClientEntitySet, ClientEntity>> response = entitySetIteratorRequest.execute();