Querying Business Partner API of SAP S/4HANA Cloud
In this exercise, you will learn how to query the Business Partner API of SAP S/4HANA Cloud. We will use the Mock Server instead of the actual SAP S/4HANA Cloud.
Prerequisites
For the complete execution of current exercise, you must execute the following activities first, using SAP Business Application Studio:
- Execute the following exercises:
- Creating your Pay-As-You-Go Account in SAP BTP
- Run the Mock Server on port 8081
- Run the following commands in a terminal, in case you didn't do it already in previous exercises, to clone the Git repository for the course:Code snippetExpand
cd ~/projects git clone https://github.com/SAP-samples/cloud-sdk-learning-journey.git
- Choose File → Open Folder and open the
/home/user/projects/cloud-sdk-learning-journey/
folder.
Steps
Checkout the branch javaspring_v1.0_query in SAP Business Application Studio
Run the following commands in the terminal to checkout the Git branch:
Build and run the project
Build the application by running the following commands in the terminal
Open the
Application.java
file. Press F5 to run the application
Query the Business Partner API of SAP S/4HANA Cloud
Open the requests.http file
Click on Send Request in the requests.http file to submit the GET request for business partners
Verify that the list of business partners are returned from the Mock Server. Note: If the Mock Server is not running, make sure to run the Mock Server on port 8081 by following the prerequisites at the top
Optional: Open BusinessPartnerController.java file in the controllers folder. Uncomment section of code to replace the URL property with https://sandbox.api.sap.com/s4hanacloud to retrieve business partners from SAP API Business Hub. Note that you also have to provide the API KEY
Optional: Uncomment section of code to replace the URL property with https://my######.s4hana.ondemand.com to retrieve business partners from SAP S/4HANA Cloud
Optional: Review code in the following source files
Note
Check out the exercise on SAP Tutorial Navigator on Connecting to OData Service on Cloud Foundry using SAP Cloud SDK. https://developers.sap.com/tutorials/s4sdk-odata-service-cloud-foundry.htmlCode walk-through
File name Comments on code changes BusinessPartnerController.java Code logic is as follows...
- Annotate the method getBusinessPartners with @RequestMapping to handle the GET method
- Create a variable named destination of type HttpDestination with information about the Mock Server or API Business Hub
- Create an instance of the DefaultBusinessPartnerService class
- Call the getAllBusinessPartner method to retrieve the business partners
- Use the SAP Cloud SDK fluent API method chaining to provide additional parameters
- Return the list of business partners as part of the GET request
requests.http Provides various HTTP requests that can be submitted to query the Business Partner API commands.txt Provides various commands that can be run on the terminal
Optional: Manually create the project. The following steps create an exact replica of the project you just checked out using Git.
Generate a Spring Boot project using SAP Cloud SDK from scratch using the Maven archetype. Run the following command in the terminal
Code snippetExpandcd ~/projects mvn archetype:generate "-DarchetypeGroupId=com.sap.cloud.sdk.archetypes" "-DarchetypeArtifactId=scp-cf-spring" "-DarchetypeVersion=RELEASE" "-DartifactId=bpstandardvdm" "-DgroupId=com.sap.cloud.sdk.tutorial" "-Dpackage=com.sap.cloud.sdk.tutorial"
Open the bpstandardvdm folder in the workspace. Create or update the following files in the table below
Build and run the project in
projects/bpstandardvdm
in the same exact way you did in the previous steps with project inprojects/cloud-sdk-learning-journey/bpstandardvdm
.