I use Apache Olingo as the OData client for the Java SDK, which I provided for the RESTful OData API. In the SDK, I want to have strongly typed classes to represent OData objects. I am having trouble implementing this easily and so I feel like there is no other strategy here.
The Olingo image seems to be to get an ODataClient object that provides the user with a bunch of useful methods for interacting with the API. ODataClient uses a bunch of factory methods to create my query. For example, this is the code I used to get Customers from the OData service for a Northwind sample. client is an instance of the required ODataClient class.
String serviceRoot = "http://services.odata.org/V4/Northwind/Northwind.svc"; URI customersUri = client.newURIBuilder(serviceRoot) .appendEntitySetSegment("Customers").build(); ODataRetrieveResponse<ODataEntitySetIterator<ODataEntitySet, ODataEntity>> response = client.getRetrieveRequestFactory().getEntitySetIteratorRequest(customersUri).execute(); if (response.getStatusCode() >= 400) { log("Error"); return; } ODataEntitySetIterator<ODataEntitySet, ODataEntity> iterator = response.getBody(); while (iterator.hasNext()) { ODataEntity customer = iterator.next(); log(customer.getId().toString()); }
I would like to get a strongly typed object from an iterator (i.e. Customer customer = iterator.next() ). However, I am not sure how to do this.
If I create a Customer class that extends ODataEntity and tries to translate, such as Customer customer = (Customer) iterator.next() , then I get a ClassCastException since the objects in the iterator are ODataEntity objects and don't know anything about the Customer subclass .
My next thought was to introduce generics, but that would require what seems like a good modification of the Olingo library, which makes me think that there is a better way to do this.
I am using Apache Olingo 4 version for development since the OData service must use OData 4.
What am I missing?
java odata olingo
Anthony elliott
source share