[英]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 Service和Connectivity 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.