Querying SAP APIs in a Java Program

After completing this lesson, you will be able to:

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

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

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