Exercise: Implementing CRUD operations in a Java Spring application

Objectives
After completing this lesson, you will be able to:

After completing this lesson, you will be able to:

  • Implementing CRUD operations in a Java Spring application

Implementing CRUD operations in a Java Spring application

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:

  1. Execute the following exercises:
    • Create your Pay-As-You-Go Account in SAP BTP
    • Run the Mock Server on port 8081
    • Complete the exercise Implementing caching in SAP Cloud SDK application

Steps

  1. Checkout the branch javaspring_v4.0_crud in SAP Business Application Studio

    1. Open SAP Business Application Studio.

    2. Run the following commands in the terminal. Make sure current working directory is /home/user/projects/cloud-sdk-learning-journey.

      Code snippet
      cd ~/projects/cloud-sdk-learning-journey
      git checkout javaspring_v4.0_crud
      Copy code
  2. Build and run the project

    1. Build the application by running the following commands in the terminal

      Code snippet
      cd bpstandardvdm
      mvn clean install -Dmaven.test.skip=true
      Copy code
    2. Open the Application.java file. Press F5 to run the application

  3. Query the Business Partner API of SAP S/4HANA Cloud

    1. Open the requests.http file

    2. Click on Send Request in the requests.http file to submit the GET request for business partners

    3. 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

    4. Click on the various Send Request in the requests.http file to submit GET, PUT, POST requests

    5. Verify that the response is correct for all the requests

  4. Optional: Review code in the following source files

    1. Code walk-through

      File nameComments on code changes
      BusinessPartnerController.java

      Code logic is as follows...

      1. Annotate the class BusinessPartnerController with @RequestMapping to handle all the HTTP methods
      2. Create a variable named destination of type HttpDestination with information about the Mock Server or API Business Hub
      3. Create 5 methods getBusinessPartners, getBusinessPartner, createBusinessPartner, updateBusinessPartner, deleteBusinessPartner to handle the GET, GET/{id}, POST, PUT, DELETE requests
      4. 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...

      1. Return list of business partners
      2. No significant code change from previous branch
      GetBusinessPartnerCommand.java

      Code logic is as follows...

      1. Receive both destination and id in the constructor argument
      2. Use the getBusinessPartnerByKey method by passing in the id value
      3. Use the SAP Cloud SDK fluent API method chaining to provide additional parameters
      UpdateBusinessPartnerCommand.java

      Code logic is as follows...

      1. Receive destination, id and business partner details in the constructor argument
      2. Use the getBusinessPartnerByKey method by passing in the id value
      3. Use the SAP Cloud SDK fluent API method chaining to provide additional parameters
      4. Replace the FIRST_NAME property
      5. Use the updateBusinessPartner method by passing in the updated business partner object
      CreateBusinessPartnerCommand.java

      Code logic is as follows...

      1. Receive destination, id and business partner details in the constructor argument
      2. Use the createBusinessPartner method by passing in the received business partner object
      3. Use the SAP Cloud SDK fluent API method chaining to provide additional parameters
      DeleteBusinessPartnerCommand.java

      Code logic is as follows...

      1. Receive both destination and id in the constructor argument
      2. Use the getBusinessPartnerByKey method by passing in the id value
      3. Use the SAP Cloud SDK fluent API method chaining to provide additional parameters
      4. Return Delete method is not available message
      requests.httpProvides various HTTP requests that can be submitted to query the Business Partner API
      commands.txtProvides various commands that can be run on the terminal
  5. Optional: Manually create the project

    1. Create or update the following files in the table below

      Note
      The following files should be added to the manually created project in the corresponding step of the previous exercise Implementing caching in SAP Cloud SDK application.
      File nameTypeContents
      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...

Save progress to your learning plan by logging in or creating an account

Login or Register