In a basic Java program, read the Business Partner API service using SAP Cloud SDK.
Prerequisites
For the complete execution of current exercise, you must execute the following activities first:
- Execute the following exercises:
- Creating your Pay-As-You-Go Account in SAP BTP
- Activating the APIs in SAP S/4HANA Cloud: this exercise activates the required API in the S/4HANA Cloud tenant.
In case you want to run this exercise in SAP Business Application Studio, just skip this step.
In case you want to run this exercise in Visual Studio Code, make sure that the following software is installed on your laptop:
Software Version Type Java Development Kit, Standard Edition 8 application Apache Maven latest application Git latest application Visual Studio Code latest application Extension Pack for Java latest Visual Studio Code Extension - Retrieve the following information you will need for the exercise:
Information How to get it API Key - Open https://api.sap.com/api/API_BUSINESS_PARTNER/overview
- Login with your user and password.
- Choose Show API Key.
SAP S/4HANA Cloud tenant URL Use the same tenant URL where you executed the Activating the APIs in SAP S/4HANA Cloud exercise. Service user name The service user you created in the previous exercise, for example ADDRESS_MANAGER_###. Password The password you created in the previous exercise, for example WelcomeToTheClouds1!. You now create the initial application project: Open your IDE (SAP Business Application Studio), then clone the course GitHub repository to a local folder.
Use the following information:
Information Value GitHub repository URL https://github.com/SAP-samples/cloud-sdk-learning-journey.git In the Get Started page, choose Clone from Git, enter the repository URL, open the cloned repository
Steps
In your IDE (SAP Business Application Studio), run the initial Hello World application.
Switch to Git branch
javaminimal_v1.0_hello
. You can choose the branch in the left corner of the bottom bar. In alternative, you can open a Terminal and run:Review the source code in the
App.java
file (undersrc/main/java/com/example
).To build the application, open a Terminal and run:
To run the application, choose F5 (Run → Start Debugging).
Modify the project so that the
App.main()
method reads the service using the SAP Cloud SDK. Extract the BUSINESS_PARTNER, LAST_NAME, FIRST_NAME for all the Business Partners whose first name begins with letter E.We modify the project in the following way:
File Changes pom.xml we include a dependency to s4hana-all
.App.java We realize the following code logic:
- Create an HttpDestination class with information about the backend system
- Create the BusinessPartnerService.
- Prepare the query, including some selection of columns and filtering of rows.
- Store the API Key in the request headers (required for use with the SAP API sandbox).
- Execute the query against the destination.
- Retrieve the returned list, convert it to JSON and print it to the console.
To get the resulting code, switch to Git branch
javaminimal_v2.0_sandbox
(use same command as in step 1).Review the changed source files.
In
App.java
, find the following line:Enter your API Key (replace
>>>YOUR APIKEY<<<
).Build and run the application.
Modify the project to query your S/4HANA Cloud system, accessible via the following information (replace ### with your user number):
Information Value URL Your S/4HANA Client tenant URL, for example https://my000000.s4hana.ondemand.com Service user name ADDRESS_MANAGER_### Password WelcomeToTheClouds1! We modify the project in the following way:
File Changes App.java We change the deatination creation with the new URL, User and password.
We remove the assignment of the API Key, since it is not required to access an SAP S/4HANA Cloud system.
To get the resulting code, switch to Git branch
javaminimal_v3.0_s4cloud
(use same command as in step 1).Review the changed source file.
In
App.java
, enter your values for URL, User and Password in the following lines.Code snippetExpand.property("URL", "https://my000000.s4hana.ondemand.com") .property("User", "ADDRESS_MANAGER_###") .property("Password", "WelcomeToTheClouds1!")
Build and run the application.