Querying SAP APIs in a Java Program

Objective

After completing this lesson, you will be able to query an SAP API using the SAP Cloud SDK in a Java program

Retrieving code snippets

Screenshot of SAP API Business Hub showing a Java code snippet for initializing data output stream and buffered reader objects, including an API endpoint URL. Navigation menu items and email address input field are visible on the left side.

In the SAP API Business Hub, code snippets are provided for every service segment for various development languages. The Java code snippets work "as is" and are able to read the service, so they offer a good starting point. Nevertheless, the given code reads the service at a low and a data model unaware level, using the standard Java.net API. The request message must be composed and include the required technical parameters. The response comes as a plain JavaScript Object Notation (JSON) content that needs to be interpreted by you. The possibility to create complex queries is very limited.

Accessing the APIs using SAP Cloud SDK

Screenshot of Computer Code and Flowchart Elements: The image displays a screenshot of computer code in a programming environment with annotations and flowchart elements. The code is related to setting up and querying a business partner service. It includes properties for destination, URL, authentication, and access token. There are comments in the code explaining the steps: ‘Preparing the destination,’ ‘Preparing the password to access the CloudService,’ and ‘Running the query.’

Using the SAP Cloud Software Development Kit (SDK), you can read the data in a way that is aware of the Virtual Data Model (VDM). Here are some examples:

  • Get the service for the business partner by creating an instance of the DefaultBusinessPartnerService() class.
  • Run "SQL like" queries across the service using specific business object aware classes.
  • The SQL equivalent of the Java code, as shown in the figure, would be something like the following:

    SELECT business_partner, last_name, first_name FROM BusinessPartner WHERE first_name = "Peter".

Log in to track your progress & complete quizzes