简体   繁体   中英

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();
    }
}

I found the above sample code but since I am new to SAP and my system is an OnPrem version deployed on AWS. I am not sure how to pass the HttpDestinationProperties destinationprop Further that method seems to be deprecated. I am looking for some sample code to call the Sales order Odata Service using the code I generated using the instructions provided. I generated the code using a maven plugin. https://sap.github.io/cloud-sdk/docs/java/features/odata/generate-typed-odata-v2-and-v4-client-for-java I am using the odata V2 Plugin

Usually you would use the SAP BTP Cloud Foundry Destination Service and Connectivity Service . Make sure to also have a Cloud Connector connected to your sub-account, it will act as reverse-proxy to the On-Premise system. You bind the destination service (eg plan lite ) as well as the connectivity service (eg plan lite ) to your application.

Once the prerequisites are met, you can use the 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);

(Of course you can boil it down to a one-liner.)

The OData call itself can be called like the following without try-catch:

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

That's it!

PS.: If you want to define your destination in static code, that would be possible with the DefaultErpHttpDestination.builder("http://your-proxy-host:44300") API. However this is obviously not best-practice.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM