【问题标题】:How to make $batch POST request using Olingo v2 and Java如何使用 Olingo v2 和 Java 发出 $batch POST 请求
【发布时间】:2018-11-09 04:09:06
【问题描述】:

我正在尝试使用 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--

【问题讨论】:

    标签: java odata olingo


    【解决方案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);
    

    您可以通过 fluent-style 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 = payload 变量)。之后,您可以使用 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 获取例如属性。

    【讨论】:

      猜你喜欢
      • 2022-10-18
      • 1970-01-01
      • 2010-11-25
      • 2014-06-20
      • 2019-09-28
      • 2016-08-25
      • 2022-11-02
      • 2013-10-22
      相关资源
      最近更新 更多