Exercise: Implementing CRUD Operations in a Java Spring Application

Objective

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

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:

  1. 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.
  2. 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 Snippet
    Copy code
    Switch to dark mode
    12
    cd ~/projects git clone https://github.com/SAP-samples/cloud-sdk-learning-journey.git
  3. Choose FileOpen Folder and open the /home/user/projects/cloud-sdk-learning-journey/ folder.

Steps

  1. Checkout the branch javaspring_v4.1_crud

    1. Checkout the javaspring_v4.1_crud Git branch by running the following commands in the terminal:

      Code Snippet
      Copy code
      Switch to dark mode
      12
      cd ~/projects/cloud-sdk-learning-journey git checkout -f javaspring_v4.1_crud
  2. Build and run the project.

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

      Code Snippet
      Copy code
      Switch to dark mode
      12
      cd bpstandardvdm mvn clean install -Dmaven.test.skip=true
    2. Run the application by running the following commands in the terminal

      Code Snippet
      Copy code
      Switch to dark mode
      12
      cd application mvn spring-boot:run

      Note

      As an alternative, to run the application, you can open the Application.java file. Choose RunStart Debugging or press F5.
  3. Make sure that the Mock Server is up and running as described in the related previous exercise.

    1. In case the Mock server is installed but not started, you need to open a new terminal window and run the following commands:

      Code Snippet
      Copy code
      Switch to dark mode
      12
      cd ~/projects/cloud-s4-sdk-book npm run start
  4. 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. Click on the various Send Request in the requests.http file to submit GET, PUT, POST requests

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

    5. Stop the running application using Ctrl + C in the corresponding terminal window.

  5. Optional: code walk-through.

    1. Review code in the following source files:

      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

Log in to track your progress & complete quizzes