【问题标题】:Sample Code to Call Odata Service from Java for onprem SAP从 Java 为 onprem SAP 调用 Odata 服务的示例代码
【发布时间】:2022-01-23 04:19:45
【问题描述】:
    /**
 * 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插件

【问题讨论】:

    标签: java odata sap-cloud-sdk


    【解决方案1】:

    通常您会使用 SAP BTP Cloud Foundry Destination ServiceConnectivity Service。确保还有一个Cloud Connector 连接到您的子帐户,它将充当本地系统的反向代理。您将destination 服务(例如计划lite)以及connectivity 服务(例如计划lite)绑定到您的应用程序。

    满足先决条件后,您可以使用SAP Cloud SDK code to resolve destinations

    // 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.:如果您想在静态代码中定义您的目的地,可以使用DefaultErpHttpDestination.builder("http://your-proxy-host:44300") API。然而,这显然不是最佳实践。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多