簡體   English   中英

從 Java 為 onprem SAP 調用 Odata 服務的示例代碼

[英]Sample Code to Call Odata Service from Java for onprem SAP

    /**
 * Calls {@code GET API_SALES_ORDER_SRV/A_SalesOrder} through
 * {@link FluentHelperRead} to get the SalesOrder events expanded to sales
 * order items and filtered by {@code keys} list
 *
 * @param keys
 *          the list of sales orders IDs to be fetched
 * @return the list of sales orders or an empty list if {@code keys} is empty
 *
 * @throws ODataException
 *           in case the request was not successful
 * @throws IllegalArgumentException
 *           in case {@code keys} is null
 *
 * @see //ProcessSalesOrderService#getAllSalesOrder()
 * @see <a href=
 *    "https://api.sap.com/shell/discover/contentpackage/SAPS4HANACloud/api/API_SALES_ORDER_SRV?resource=A_SalesOrder&operation=get_A_SalesOrder">SAP
 *    API Business Hub</a> for details of
 *    {@code GET API_SALES_ORDER_SRV/A_SalesOrder} endpoint
 */
public List<SalesOrderHeader> getByKeys(@NonNull Collection<String> keys) throws IllegalArgumentException, ODataException {
    if (keys.size() == 0) {
        return Collections.emptyList();
    }
    
    
    // create OData $filter with all keys 
    final ExpressionFluentHelper<SalesOrderHeader> filter = keys.stream()
            .map(key -> SalesOrderHeader.SALES_ORDER.eq(key))
            .reduce(ExpressionFluentHelper::or)
            .get();

    try {
        HttpDestinationProperties destinationprop = null;
        return salesOrderService.getAllSalesOrder()
            .select(SalesOrderHeader.ALL_FIELDS, SalesOrderHeader.TO_ITEM)
            .filter(filter)
            .execute(destinationprop );
    } catch (com.sap.cloud.sdk.odatav2.connectivity.ODataException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

我找到了上面的示例代碼,但由於我是 SAP 新手,而且我的系統是部署在 AWS 上的 OnPrem 版本。 我不確定如何傳遞 HttpDestinationProperties destinationprop 此外,該方法似乎已被棄用。 我正在尋找一些示例代碼來使用我使用提供的說明生成的代碼來調用銷售訂單 Odata 服務。 我使用 maven 插件生成了代碼。 https://sap.github.io/cloud-sdk/docs/java/features/odata/generate-typed-odata-v2-and-v4-client-for-java我正在使用odata V2插件

通常您會使用 SAP BTP Cloud Foundry Destination ServiceConnectivity Service 確保還有一個雲連接器連接到您的子帳戶,它將充當本地系統的反向代理。 您將destination服務(例如 plan lite )以及connectivity服務(例如 plan lite )綁定到您的應用程序。

滿足先決條件后,您可以使用SAP Cloud SDK 代碼來解析目標

// General API usage:
Destination destination = DestinationAccessor.getDestination("my-destination");

// Cast to HTTP destination:
HttpDestination httpDestination = destination.asHttp(); 

// Apply dedicated ERP logic for SAP S/4HANA
HttpDestination erpHttpDestination = httpDestination.decorate(DefaultErpHttpDestination::new);

(當然,您可以將其歸結為單線。)

OData 調用本身可以在沒有 try-catch 的情況下調用如下:

return salesOrderService.getAllSalesOrder()
  .select(SalesOrderHeader.ALL_FIELDS, SalesOrderHeader.TO_ITEM)
  .filter(filter)
  .executeRequest( erpHttpDestination );

而已!

PS.:如果您想在 static 代碼中定義您的目的地,則可以使用DefaultErpHttpDestination.builder("http://your-proxy-host:44300") API。 然而,這顯然不是最佳實踐。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM