Exercise: Querying SAP APIs in a Java Spring Application

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

After completing this lesson, you will be able to:

  • Querying SAP APIs in a Java Spring application

Querying SAP APIs in a Java Spring application

Querying Business Partner API of SAP S/4HANA Cloud

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:

  1. Execute the following exercises:
    • Creating your Pay-As-You-Go Account in SAP BTP
    • Run the Mock Server on port 8081
  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
    cd ~/projects
    git clone https://github.com/SAP-samples/cloud-sdk-learning-journey.git
    Expand
  3. Choose FileOpen Folder and open the /home/user/projects/cloud-sdk-learning-journey/ folder.

Steps

  1. Checkout the branch javaspring_v1.0_query in SAP Business Application Studio

    1. Run the following commands in the terminal to checkout the Git branch:

      Code snippet
      cd cloud-sdk-learning-journey
      git checkout -f javaspring_v1.0_query
      Expand
  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
      Expand
    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. Optional: Open BusinessPartnerController.java file in the 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

    5. Optional: Uncomment section of code to replace the URL property with https://my######.s4hana.ondemand.com to retrieve business partners from SAP S/4HANA Cloud

  4. Optional: Review code in the following source files

    1. Note
      Check out 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

      Code walk-through

      File nameComments on code changes
      BusinessPartnerController.java

      Code logic is as follows...

      1. Annotate the method getBusinessPartners with @RequestMapping to handle the GET method
      2. Create a variable named destination of type HttpDestination with information about the Mock Server or API Business Hub
      3. Create an instance of the DefaultBusinessPartnerService class
      4. Call the getAllBusinessPartner method to retrieve the business partners
      5. Use the SAP Cloud SDK fluent API method chaining to provide additional parameters
      6. Return the list of business partners as part of the GET request
      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. The following steps create an exact replica of the project you just checked out using Git.

    1. Generate a Spring Boot project using SAP Cloud SDK from scratch using the Maven archetype. Run the following command in the terminal

      Code snippet
      cd ~/projects
      mvn archetype:generate "-DarchetypeGroupId=com.sap.cloud.sdk.archetypes" "-DarchetypeArtifactId=scp-cf-spring" "-DarchetypeVersion=RELEASE" "-DartifactId=bpstandardvdm" "-DgroupId=com.sap.cloud.sdk.tutorial" "-Dpackage=com.sap.cloud.sdk.tutorial"
      Expand
    2. Open the bpstandardvdm folder in the workspace. Create or update the following files in the table below

      File nameTypeContents
      BusinessPartnerController.java

      Path: ~/projects/bpstandardvdm/application/src/main/java/com/sap/cloud/sdk/tutorial/controllers

      New

      Copy contents from here...

      requests.http

      Path: ~/projects/bpstandardvdm

      New

      Copy contents from here...

    3. Build and run the project in projects/bpstandardvdm in the same exact way you did in the previous steps with project in projects/cloud-sdk-learning-journey/bpstandardvdm.

Log in to track your progress & complete quizzes