Skip to Content

Table of Contents

    1. Introduction

    2. Getting Started

    3. Setup Eclipse

    4. Http Client

1. Introduction


This article is the first in a three-part series that will show you how to consume (read only) an Employee Central (EC) OData Service with support of the Olingo library, the OData4j library or the http Client. We will demonstrate the use of common OData features such as $filter, $select, $expand using these libraries.

OData is an OASIS standard which enables the creation and consumption for RESTful APIs. For more information on OData, check out: http://www.odata.org/.

For the sandbox system, we will be using salesdemo4, an OData service for the HR Cloud offering of Successfactors, a SAP company, containing a suite of applications for your most demanding HR-processes.

Let’s understand how we can do this using a simple query using first name, last name, date of birth and, country of birth. To complete this example, there are certain steps that must be performed before executing the query. These are described in Sections 2 and 3.

In this three-part series, we will look at how you can consume Employee Central OData APIs in Java. This first part covers general information and the usage of HTTP Client, the second part will cover the OData4j library and the third part will cover the Olingo library.

This documentation includes snippets of source code that show you how to create a basic query. You can view the full source at: http://www.sdn.sap.com/irj/scn/index?rid=/library/uuid/a0b41a7b-8ddc-3210-5e81-f9c9cddc9ec1

Before starting consumption in Java, ensure your OData APIs can be consumed by a REST client. You can use a tool like the “Advanced REST Client” in Google Chrome

for details see:

http://scn.sap.com/community/erp/hcm/blog/2015/03/11/integration-technologies-for-successfactors-employee-central

http://scn.sap.com/community/erp/hcm/blog/2015/03/23/how-to-create-an-employee-using-odata-apis


2. Getting Started


This output template shows a query example. In the field personIdExternal field, you specify the external ID of an employee in Employee Central. Next, you choose the library to use. You can choose between Http Client, OData4j or Olingo as library to query. This example shows Olingo. Click the “GO!” Button to get your results for the two templates below. This example shows personal and biographical information of the user with the personIdExternal “cgrant1”.

ausgabe.PNG

This template shows the OData hierarchy of the example. For more information on the structure of EC OData APIs, check out: http://help.sap.com/hr_api/ and refer to the section Employee Central Entity Relationships.

hierarchy.PNG


Basic Authentication

To mimic logging in by the UI, the server needs information about username, company and password. You need to encode these three in the following format: <username>@<company>:<password> with Base64.

For example root@ACE:secret results in cm9vdEBBQ0U6c2VjcmV0.


Simple OData call

This example shows a simple OData call in Google Chrome Advanced Rest Client and its result.

AdvancedRestClient_query.PNG

This call filters the personIdExternal as “cgrant1” and selects the fields “firstName” and “lastName”.

The following snippet shows the result in JSON format.

advancedRestClient_result.PNG


OData Features

The subsequent examples will illustrate how you can consume OData in Java. These examples show how you can use OData features like expand, filter and select to specify your query.



3.Setup Eclipse


  • Install Eclipse IDE for Java EE (e.g. Kepler):

https://eclipse.org/downloads/packages/eclipse-ide-java-ee-developers/keplerr

  • Install Java SDK :

http://www.oracle.com/technetwork/java/javase/downloads/jdk7-downloads-1880260.html

  • Download the OData4j library and extract the files:

https://bintray.com/odata4j/odata4j/odata4j-archive/0.7/view

  • Download the Olingo library for OData2 and extract the files:

http://olingo.apache.org/doc/odata2/download.html

  • Copy the folder <<program.zip>> to your workspace folder and import it via “Existing projects into Workspace”
  • Add the downloaded OData4j Bundle and the downloaded Olingo Bundles to “Libraries” by right clicking on the project and selecting properties –> Java Build Path –> libraries
  • If you work in a secured network it might be necessary to configure your proxy settings. Right click your project and select properties –> Run/Debug Settings –> select your project –> Edit –> Arguments –> enter the following VM arguments:

-Dhttps.proxyHost=proxy -Dhttps.proxyPort=8080

-Dhttp.proxyHost=proxy -Dhttp.proxyPort=8080

-Dhttp.nonProxyHosts=”localhost|127.0.0.1″

These arguments apply only for people on a SAP network.

proxySettings.PNG

  • Configure your login details in the source code. You can find necessary Strings in the class QueryParams. You need to set the USER in the format <user@company> and save your password as the String PW.

Now you should be able to build the program and execute it!!


4. HTTP Client


The HTTP Client is probably the easiest way for simple OData APIs queries. The steps are as follows:


    1. Define a connection

    2. Send a call

    3. Analyze the response xml file and parse it to the output


The HTTP Client works without using the metadata structure. This has several consequences. If you only want to query small amounts of data it’s faster than the other two presented possibilities because you don’t have to download the metadata first.

Another issue is that you have to know the exact XML structure to create the URL you want to query. So, it is pretty complicated because the XML structure is not equivalent to the OData structure.

This code snippet shows how to define the connection with the server.

buildCon.PNG

This snippet shows how to send a call based on the prepared connection. The Response is in XML format, so you use its nodes to navigate through its structure to get the required information. If a non-existing personIdExternal is typed, the method throws an exception.

parseData.PNG

I hope this blog entry will help you to understand how to do the first steps to consume OData with Java.

I’m interested in your feedback on this functionality!


Best Regards Jonas

To report this post you need to login first.

1 Comment

You must be Logged on to comment or reply to a post.

  1. Christoph Scheibe

    Hi,

    thanks for the tutorial, but please next time dont use pictures and insert the code 🙂

    <code>

    String code = “Basic ” + new String(Base64.encodeBase64((“User:password”).getBytes()));
    URL absU = new URL(serviceUrl);
    URLConnection test = absU.openConnection();
    HttpURLConnection con = (HttpURLConnection) test;

    con.setRequestMethod(“GET”);
    con.setRequestProperty(“Authorization”, code);
    return new BufferedReader(new InputStreamReader(con.getInputStream()));

    </code>

    Greetings
    Christoph Scheibe

    (0) 

Leave a Reply