如何使用Olingo v2和Java进行$ batch POST请求

时间:2018-05-30 10:29:20

标签: java odata olingo

我正在尝试使用OData v2在Java中执行$ batch请求。

来自浏览器的示例请求将在下面的双引号之间。 但是,如何以编程方式提出此请求?某个地方是否有样品通话?任何帮助表示赞赏。

Request URL: https://someUrl/project/odata/project/FOLDER/$batch
Request Method: POST
Status Code: 202 Accepted
Remote Address: 1.2.3.4:1234
Referrer Policy: no-referrer-when-downgrade
content-encoding: gzip
content-length: 5256
content-type: multipart/mixed; boundary=E828EB257B134AC6F567C8D3B67E666E1
dataserviceversion: 2.0
Accept: multipart/mixed
Accept-Encoding: gzip, deflate, br
Accept-Language: en
Connection: keep-alive
Content-Length: 595
Content-Type: multipart/mixed;boundary=batch_4edb-a2cd-948d
Cookie: project-usercontext=project-language=EN&project-client=100; 
--Some cookie content--

DataServiceVersion: 2.0
Host: host.myClient.com:1234
MaxDataServiceVersion: 2.0
Origin: https://host.myClient.com:1234
Referer: https://host.myClient.com:1234/project/index.html
project-cancel-on-close: true
project-contextid-accept: header
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/1.2.3.4 Safari/537.36
x-csrf-token: 8Fd53yy2vuCjnaFKrZNuLg==
--batch_4edb-a2cd-948d
Content-Type: application/http
Content-Transfer-Encoding: binary

GET MyEntityDetailsSet HTTP/1.1
project-contextid-accept: header
Accept: application/json
Accept-Language: en
DataServiceVersion: 2.0
MaxDataServiceVersion: 2.0
project-cancel-on-close: true


> --batch_4edb-a2cd-948d
Content-Type: application/http
Content-Transfer-Encoding: binary

GET MyObjectSet HTTP/1.1
project-contextid-accept: header
Accept: application/json
Accept-Language: en
DataServiceVersion: 2.0
MaxDataServiceVersion: 2.0
project-cancel-on-close: true


--batch_4edb-a2cd-948d--

1 个答案:

答案 0 :(得分:1)

你可以使用Olingo V2作为OData客户端(虽然在我看来是一个相当丑陋的客户端)。在Olingo官方网站上有一个专门用于此用法的完整教程:How to use Apache Olingo as client library

Olingo知道构建请求和解析响应,但是您需要一个底层机制来执行HTTP调用。我的建议是不要像上面的例子那样依赖手动打开HttpURLConnections,而是使用类似Apache Http Client或其他专用库的东西(为了减少你编写的代码量,也可以访问更高级的概念,如连接轮询)。

简而言之,您必须首先阅读并解析您要使用的服务的元数据:

// content = read the metadata as an InputStream
Edm dataModel = EntityProvider.readMetadata(content, false);

您可以通过流畅的API构建批处理请求:

BatchQueryPart part = BatchQueryPart.method("GET")
    .uri("/Employees('1')")
    .build();

// here you could have a larger list of parts, not just a singleton list
InputStream payload = EntityProvider.writeBatchRequest(
    Collections.singletonList(part), "batch_boundary");

然后你必须使用你选择的HTTP请求执行机制(method =“POST”和body =有效载荷变量)来执行它。之后,您可以使用Olingo解析获得的响应:

// body = the response body received
// contentType = the Content-Type header received
List<BatchSingleResponse> responses = 
     EntityProvider.parseBatchResponse(responseBody, contentType);

// you can obtain the body for each request from the response list
String partBody = responses.get(0).getBody(); 
InputStream partStream = new ByteArrayInputStream(partBody.getBytes());
String partType = responses.get(0).getHeader(HttpHeaders.CONTENT_TYPE);

最后,使用第一步中的Edm,您还可以根据您构建的请求类型解析每个单独的主体。例如,您可以使用readEntry方法反序列化单个实体读取:

// first we have to find the entity set you used to make the request
EdmEntitySet entitySet =  edm.getDefaultEntityContainer()
     .getEntitySet("Employees");
ODataEntry entry = EntityProvider.readEntry(partType, entitySet, 
     partStream, EntityProviderReadProperties.init().build())

最后,您可以使用entry methods来获取,例如属性。