Olingo - Create Strongly Typed POJOs for the OData Service Client Library

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?

+7
java odata olingo
source share
1 answer

It really isn't advertised, but there is currently a POJO generator in Olingo, in the source tree in the ext / pojogen-maven plugin. Unfortunately, to use POJO, another layer with a different programming model has been added, which stores objects cached in memory and synchronizes with the OData service during the cleanup operation. I would be very interested in adapting it to a more traditional request / response model based on Olingos Request Factories.

However, you can try. Your pom includes pojogen-maven-plugin and odata-client-proxy. POJO generation can be run in pom using

  <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>build-helper-maven-plugin</artifactId> <version>1.8</version> <executions> <execution> <phase>process-sources</phase> <goals> <goal>add-source</goal> </goals> <configuration> <sources> <source>${project.build.directory}/generated-sources</source> </sources> </configuration> </execution> </executions> </plugin> <plugin> <groupId>org.apache.olingo</groupId> <artifactId>pojogen-maven-plugin</artifactId> <version>4.2.0-SNAPSHOT</version> <configuration> <outputDirectory>${project.build.directory}/generated-sources</outputDirectory> <localEdm>${basedir}/src/main/resources/metadata.xml</localEdm> <basePackage>odata.test.pojo</basePackage> </configuration> <executions> <execution> <id>v4pojoGen</id> <phase>generate-sources</phase> <goals> <goal>v4pojoGen</goal> </goals> </execution> </executions> </plugin> </plugins> 

In the experiment, I saved the EDM metadata from the Olingo Car service example in the src / main / resources / metadata.xml file. Somehow, the plugin wants to create an intermediate ojc-plugin folder, and I just transferred the generated Java code to the right place manually.

At this point, you have the Service.java and Java interfaces for each object or complex type in the EDM model.

You can use it to read some objects, such as

 Service<EdmEnabledODataClient> service = odata.test.pojo.Service.getV4("http://localhost:9080/odata-server-sample/cars.svc"); Container container = service.getEntityContainer(Container.class); for (Manufacturer m : container.getManufacturers()) { System.out.println(m.getName()); } 
+4
source share

All Articles