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 free trial account in SAP BTP
- Setting up the mock server, so that you have the Mock server program available in the cloud-s4-sdk-book folder.
- 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 Snippet12cd ~/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.2_query in SAP Business Application Studio
Checkout the Git branch by running the following commands in the terminal:
Code Snippet12cd ~/projects/cloud-sdk-learning-journey git checkout -f javaspring_v1.2_query
Build and run the project
Build the application by running the following commands in the terminal
Code Snippet12cd bpstandardvdm mvn clean install -Dmaven.test.skip=trueStart the application by running the following commands in the terminal
Code Snippet12cd application mvn spring-boot:runNote
On the execution of the mvn command, we verified sometimes a "Compilation failure" error. As a workaround, just re-execute the command and the application will start correctly.Note
As an alternative, to run the application, you can open theApplication.java
file. Choose Run → Start Debugging or press F5.
Make sure that the Mock Server is up and running as described in the related previous exercise.
In case the Mock server is installed but not started, you need to open a new terminal window and run the following commands:
Code Snippet12cd ~/projects/cloud-s4-sdk-book npm run start
Query the Business Partner API of the Mock Server
Open the requests.http file
Click on Send Request in the requests.http file to submit the GET request for business partners
Stop the running application using Ctrl + C in the corresponding terminal window.
Optional: Query the Business Partner API of the sandbox in api.sap.com.
Access https://api.sap.com/api/API_BUSINESS_PARTNER. Login and get your API Key.
Open BusinessPartnerController.java file in the ~/projects/cloud-sdk-learning-journey/bpstandardvdm/application/src/main/java/com/sap/cloud/sdk/tutorial/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
Re-build and run the application:
Code Snippet12345cd ~/projects/cloud-sdk-learning-journey/bpstandardvdm mvn clean install -Dmaven.test.skip=true cd application mvn spring-boot:runRe-test the Business Partners query in the requests.http file.
Stop the running application using Ctrl + C in the corresponding terminal window.
Optional: Query the Business Partner API of your SAP S/4HANA Cloud, public edition tenant.
Uncomment section of code to replace the URL property with https://my######.s4hana.ondemand.com.
Enter proper URL, User and Password. The API Key is not required.
Re-build and run the application.
Re-test the Business Partners query in the requests.http file.
Stop the running application using Ctrl + C in the corresponding terminal window.
Optional: code walk-through.
review code in the following source files:
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 Note
Review 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.html
Optional: Manually re-create the project in ~/projects/bpstandardvdm.
The following steps create an exact replica of the project you just checked out from Git. Step-by-step, without 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 Snippet12cd ~/projects mvn archetype:generate "-DarchetypeGroupId=com.sap.cloud.sdk.archetypes" "-DarchetypeArtifactId=spring-boot3" "-DarchetypeVersion=RELEASE" "-DartifactId=bpstandardvdm" "-DgroupId=com.sap.cloud.sdk.tutorial" "-Dpackage=com.sap.cloud.sdk.tutorial"Open the bpstandardvdm folder in the workspace.
Delete the whole application/src/test subfolder.
Create or update the files in the table below
File name Type Contents API_BUSINESS_PARTNER.edmx Path: ~/projects/bpstandardvdm/application/edmx/
New Copy contents from here pom.xml Path: ~/projects/bpstandardvdm/application/
Update Copy contents from here BusinessPartnerController.java Path: ~/projects/bpstandardvdm/application/src/main/java/com/sap/cloud/sdk/tutorial/controllers/
New Copy contents from here...
serviceNameMappings.properties ~/projects/bpstandardvdm/
New Copy contents from here...
requests.http Path: ~/projects/bpstandardvdm/
New Copy contents from here...
Build and run the project in projects/bpstandardvdm.
Code Snippet12345cd ~/projects/bpstandardvdm mvn clean install -Dmaven.test.skip=true cd application mvn spring-boot:runNote
On the execution of the mvn command, we verified sometimes a "Compilation failure" error. As a workaround, just re-execute the command and the application will start correctly.Note
Remove theapplication/src/test
folder if it exists in the main folder before running mvn spring-boot:runStop the running application using Ctrl + C in the corresponding terminal window.