This article is the third and last in a three-part series. In this blog, I will briefly explain how to consume OData with the OData4j library. You can find the full source code in the first article of this serie: Consuming Employee Central OData Services with Java
If you want to learn more about consuming OData with the Java OData4j library see: Consuming Employee Central OData Services with the Java OData4j library
Apache Olingo is an open source Java library which implements OData. It is part of the technical foundation of SAP Gateway technology. You can use Olingo for Client or Server usage.
The steps to consume OData in Java are as follows:
1. Download the metadata and define connection
2. Read Feed
3. Analyze data and parse it to the output
This code snippet shows the Olingo method readEdm which reads the metadata of the given service URL.
With help of metadata being made available via class EDM you can now read a feed which is done in the following code snippet. In this code snippet the final access URL is created by adding filter, select and expand.
With the absolute URI you can initialize an input stream. This process includes the basic authentication and http status code checkup.
The next step is to analyze the feed. The method isEmpty controls that the feed is not empty. After that you loop over the entries.
The method parseData initializes the parse process by looping over the properties of each entry.
The method handleEntry writes the name and value of attribute in the data model. If the attribute is empty the method writes the String “null” as value. If the entry has <<PerPerson>> as entity, a specified handleEntry is launched.