Exercise: Querying SAP APIs in a minimal Java program

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

After completing this lesson, you will be able to:

  • Within a minimal Java program, call a SAP API and retrieve data from SAP S/4HANA Cloud

Querying SAP APIs in a minimal Java program

In a basic Java program, read the Business Partner API service using SAP Cloud SDK.

Prerequisites

For the complete execution of current exercise, you must execute the following activities first:

  1. Execute the following exercises:
    • Creating your Pay-As-You-Go Account in SAP BTP
    • Activating the APIs in SAP S/4HANA Cloud: this exercise activates the required API in the S/4HANA Cloud tenant.
  2. In case you want to run this exercise in SAP Business Application Studio, just skip this step.

    In case you want to run this exercise in Visual Studio Code, make sure that the following software is installed on your laptop:

    SoftwareVersionType
    Java Development Kit, Standard Edition8application
    Apache Mavenlatestapplication
    Gitlatestapplication
    Visual Studio Codelatestapplication
    Extension Pack for JavalatestVisual Studio Code Extension
  3. Retrieve the following information you will need for the exercise:
    InformationHow to get it
    API Key
    1. Open https://api.sap.com/api/API_BUSINESS_PARTNER/overview
    2. Login with your user and password.
    3. Choose Show API Key.
    SAP S/4HANA Cloud tenant URLUse the same tenant URL where you executed the Activating the APIs in SAP S/4HANA Cloud exercise.
    Service user nameThe service user you created in the previous exercise, for example ADDRESS_MANAGER_###.
    PasswordThe password you created in the previous exercise, for example WelcomeToTheClouds1!.
  4. You now create the initial application project: Open your IDE (SAP Business Application Studio), then clone the course GitHub repository to a local folder.

    Use the following information:

    InformationValue
    GitHub repository URLhttps://github.com/SAP-samples/cloud-sdk-learning-journey.git

    choose Clone from Git, enter the repository URL, open the cloned repository

Steps

  1. In your IDE (SAP Business Application Studio), run the initial Hello World application.

    1. Switch to Git branch javaminimal_v1.0_hello. You can choose the branch in the left corner of the bottom bar. In alternative, you can open a Terminal and run:

      Code snippet
      git checkout -f javaminimal_v1.0_hello
      Copy code
    2. Review the source code in the App.java file.

    3. To build the application, open a Terminal and run:

      Code snippet
      mvn clean install
      Copy code
    4. To run the application, choose F5 (RunStart Debugging).

  2. Modify the project so that the App.main() method reads the service using the SAP Cloud SDK. Extract the BUSINESS_PARTNER, LAST_NAME, FIRST_NAME for all the Business Partners whose first name begins with letter E.

    1. We modify the project in the following way:

      FileChanges
      pom.xmlwe include a dependency to s4hana-all.
      App.java

      We realize the following code logic:

      1. Create an HttpDestination class with information about the backend system
      2. Create the BusinessPartnerService.
      3. Prepare the query, including some selection of columns and filtering of rows.
      4. Store the API Key in the request headers (required for use with the SAP API sandbox).
      5. Execute the query against the destination.
      6. Retrieve the returned list, convert it to JSON and print it to the console.

      To get the resulting code, switch to Git branch javaminimal_v2.0_sandbox.

    2. Review the changed source files.

    3. In App.java, find the following line:

      Code snippet
      helper.withHeader("apikey", ">>>YOUR APIKEY<<<");
      Copy code

      Enter your API Key (replace >>>YOUR APIKEY<<<).

    4. Build and run the application.

  3. Modify the project to query your S/4HANA Cloud system, accessible via the following information (replace ### with your user number):

    InformationValue
    URLYour S/4HANA Client tenant URL, for example https://my000000.s4hana.ondemand.com
    Service user nameADDRESS_MANAGER_###
    PasswordWelcomeToTheClouds1!
    1. We modify the project in the following way:

      FileChanges
      App.java

      We change the deatination creation with the new URL, User and password.

      We remove the assignment of the API Key, since it is not required to access an SAP S/4HANA Cloud system.

    2. To get the resulting code, switch to Git branch javaminimal_v3.0_s4cloud.

    3. Review the changed source file.

    4. In App.java, enter your values for URL, User and Password in the following lines.

      Code snippet
          .property("URL", "https://my000000.s4hana.ondemand.com")
          .property("User", "ADDRESS_MANAGER_###")
          .property("Password", "WelcomeToTheClouds1!")
      Copy code
    5. Build and run the application.

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

Login or Register