您是否有使用CUD ODataXXXRequestBuilders的示例

时间:2018-02-22 16:21:47

标签: s4sdk

在所有文档中,我找到了ODataQueryBuilder的示例。 但是,您是否也有一个示例如何使用com.sap.cloud.sdk.odatav2.connectivity包的Create,Update和Delete方法:

  • ODataCreateRequestBuilder
  • ODataDeleteRequestBuilder
  • ODataUpdateRequestBuilder

如何处理CSRF令牌?

请提供一个工作示例?

1 个答案:

答案 0 :(得分:10)

在OData服务的元数据端点上使用HEAD请求获取CSRF令牌。

一些注意事项:

  • 以下示例假设您的目标名为" DestinationName"在SAP Cloud Platform主控室中配置。
  • 请注意,S/4HANA virtual data model通常是更容易的选择。

<强> ODataCreateRequestBuilder

Map<String, Object> body = new HashMap<>();
body.put("FirstName", "John");
body.put("LastName", "Doe");
body.put("BusinessPartnerCategory", "1");

ODataCreateRequest createRequest =
    ODataCreateRequestBuilder
        .withEntity("/sap/opu/odata/sap/API_BUSINESS_PARTNER", "A_BusinessPartner")
        .withBodyAsMap(body)
        .build();

createRequest.execute("DestinationName");                 

<强> ODataUpdateRequestBuilder

Map<String, Object> keys = new HashMap<>();
keys.put("BusinessPartner", "12345");

Map<String, Object> params = new HashMap<>();
params.put("FirstName", "John");
params.put("MiddleName", "D.");
params.put("LastName", "Doe");
params.put("BusinessPartnerCategory", "1");

final ODataUpdateRequest updateRequest =
    ODataUpdateRequestBuilder
        .withEntity("/sap/opu/odata/sap/API_BUSINESS_PARTNER", "A_BusinessPartner", keys)
        .withBodyAsMap(params)
        .build();

updateRequest.execute("DestinationName");      

<强> ODataDeleteRequestBuilder

Map<String, Object> keys = new HashMap<>();
keys.put("BusinessPartner", "12345");
keys.put("AddressID", "98765");

ODataDeleteRequest deleteRequest =
    ODataDeleteRequestBuilder
        .withEntity("/sap/opu/odata/sap/API_BUSINESS_PARTNER", "A_BusinessPartnerAddress", keys)
        .build();

deleteRequest.execute("DestinationName");