Implementing CRUD operations in SAP Cloud SDK application
In this exercise, you will learn how to implement CRUD operations in SAP Cloud SDK application. 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:
- Create 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_v4.0_crud
Run the following commands in the terminal. Make sure current working directory is /home/user/projects/cloud-sdk-learning-journey.
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
Click on the various Send Request in the requests.http file to submit GET, PUT, POST requests
Verify that the response is correct for all the requests
Optional: Review code in the following source files
Code walk-through
File name Comments on code changes BusinessPartnerController.java Code logic is as follows...
- Annotate the class BusinessPartnerController with @RequestMapping to handle all the HTTP methods
- Create a variable named destination of type HttpDestination with information about the Mock Server or API Business Hub
- Create 5 methods getBusinessPartners, getBusinessPartner, createBusinessPartner, updateBusinessPartner, deleteBusinessPartner to handle the GET, GET/{id}, POST, PUT, DELETE requests
- Create an instance of the utility class to handle each of the method implementation by passing in the destination and other required values.
GetBusinessPartnersCommand.java Code logic is as follows...
- Return list of business partners
- No significant code change from previous branch
GetBusinessPartnerCommand.java Code logic is as follows...
- Receive both destination and id in the constructor argument
- Use the getBusinessPartnerByKey method by passing in the id value
- Use the SAP Cloud SDK fluent API method chaining to provide additional parameters
UpdateBusinessPartnerCommand.java Code logic is as follows...
- Receive destination, id and business partner details in the constructor argument
- Use the getBusinessPartnerByKey method by passing in the id value
- Use the SAP Cloud SDK fluent API method chaining to provide additional parameters
- Replace the FIRST_NAME property
- Use the updateBusinessPartner method by passing in the updated business partner object
CreateBusinessPartnerCommand.java Code logic is as follows...
- Receive destination, id and business partner details in the constructor argument
- Use the createBusinessPartner method by passing in the received business partner object
- Use the SAP Cloud SDK fluent API method chaining to provide additional parameters
DeleteBusinessPartnerCommand.java Code logic is as follows...
- Receive both destination and id in the constructor argument
- Use the getBusinessPartnerByKey method by passing in the id value
- Use the SAP Cloud SDK fluent API method chaining to provide additional parameters
- Return Delete method is not available message
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.
Note
Before you execute this step, you must execute the corresponding optional step, described in the previous Implementing caching in a Java Spring application exercise.Create or update the following files in the table below
File name Type Contents BusinessPartnerController.java Path: ~/projects/bpstandardvdm/application/src/main/java/com/sap/cloud/sdk/tutorial/controllers
Modified Copy contents from here...
GetBusinessPartnersCommand.java Path: ~/projects/bpstandardvdm/application/src/main/java/com/sap/cloud/sdk/tutorial/utils
Modified Copy contents from here...
GetBusinessPartnerCommand.java Path: ~/projects/bpstandardvdm/application/src/main/java/com/sap/cloud/sdk/tutorial/utils
New Copy contents from here...
CreateBusinessPartnerCommand.java Path: ~/projects/bpstandardvdm/application/src/main/java/com/sap/cloud/sdk/tutorial/utils
New Copy contents from here...
UpdateBusinessPartnerCommand.java Path: ~/projects/bpstandardvdm/application/src/main/java/com/sap/cloud/sdk/tutorial/utils
New Copy contents from here...
DeleteBusinessPartnerCommand.java Path: ~/projects/bpstandardvdm/application/src/main/java/com/sap/cloud/sdk/tutorial/utils
New Copy contents from here...
requests.http Path: ~/projects/bpstandardvdm
Modified Copy contents from here...
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
.