Exercise: Querying SAP APIs in a Java Spring Application

Objectives

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

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 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
    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.1_query in SAP Business Application Studio

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

      Code snippet
      cd ~/projects/cloud-sdk-learning-journey
      git checkout -f javaspring_v1.1_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. Start the application by running the following commands in the terminal

      Code snippet
      cd application
      mvn spring-boot:run
      Expand
      Note
      On the execution of the mvn command, we verified sometimes a "Compilation failure" error. As a workaround, just re-execute the command and the application will start correctly.
      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
      cd ~/projects/cloud-s4-sdk-book
      npm run start
      Expand
  4. Query the Business Partner API of the Mock Server

    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. Stop the running application using Ctrl + C in the corresponding terminal window.

  5. Optional: Query the Business Partner API of the sandbox in api.sap.com.

    1. Access https://api.sap.com/api/API_BUSINESS_PARTNER. Login and get your API Key.

    2. Open BusinessPartnerController.java file in the ~/projects/cloud-sdk-learning-journey/bpstandardvdm/application/src/main/java/com/sap/cloud/sdk/tutorial/controllers/ folder.

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

    4. Re-build and run the application:

      Code snippet
      cd ~/projects/cloud-sdk-learning-journey/bpstandardvdm
      mvn clean install -Dmaven.test.skip=true
      cd application
      mvn spring-boot:run
      
      Expand
    5. Re-test the Business Partners query in the requests.http file.

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

  6. Optional: Query the Business Partner API of your SAP S/4HANA Cloud, public edition tenant.

    1. Uncomment section of code to replace the URL property with https://my######.s4hana.ondemand.com.

    2. Enter proper URL, User and Password. The API Key is not required.

    3. Re-build and run the application.

    4. Re-test the Business Partners query in the requests.http file.

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

  7. 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 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
      Note
      Review 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
  8. Optional: Manually re-create the project in ~/projects/bpstandardvdm.

    1. The following steps create an exact replica of the project you just checked out from Git. Step-by-step, without using Git.

    2. 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
    3. Open the bpstandardvdm folder in the workspace.

    4. Create or update the files in the table below

      File nameTypeContents
      API_BUSINESS_PARTNER.edmx

      Path: ~/projects/bpstandardvdm/application/edmx/

      NewCopy contents from here
      pom.xml

      Path: ~/projects/bpstandardvdm/application/

      UpdateCopy contents from here
      BusinessPartnerController.java

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

      New

      Copy contents from here...

      serviceNameMappings.properties

      ~/projects/bpstandardvdm/

      New

      Copy contents from here...

      requests.http

      Path: ~/projects/bpstandardvdm/

      New

      Copy contents from here...

    5. Build and run the project in projects/bpstandardvdm.

      Code snippet
      cd ~/projects/bpstandardvdm
      mvn clean install -Dmaven.test.skip=true
      cd application
      mvn spring-boot:run
      
      Expand
      Note
      On the execution of the mvn command, we verified sometimes a "Compilation failure" error. As a workaround, just re-execute the command and the application will start correctly.
    6. Stop the running application using Ctrl + C in the corresponding terminal window.

Log in to track your progress & complete quizzes