Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I consume an OData4 service in Java using either Olingo or the SDL OData Framework

Tags:

java

odata

olingo

I need to consume an OData4 service from Java and based on the list of frameworks on the OData website the two choices are Olingo or the SDL Odata Framework. My problem is that the documentation for both of these projects are focused on writing a service not consuming one. The Olingo site links to a blog post from 2014 which is not API compatible with the current version and I couldn't find anything on the SDL github pages.

If someone could just provide me with a simple POST / GET example with using a proper POJO object model that would be great.

My limited understanding is that OData moves any information about the actual object model from compile time to runtime on the client. I am happy to ignore this and code against a fixed object model because the service we are using won't change.

like image 795
Jon Freedman Avatar asked Feb 09 '16 17:02

Jon Freedman


People also ask

What is Olingo in OData?

Apache Olingo is a Java library that implements the Open Data Protocol (OData). Apache Olingo serves client and server aspects of OData. It currently supports OData 2.0 and will also support OData 4.0. The latter is the OASIS version of the protocol: OASIS Open Data Protocol (OData) TC.

How use OData connected service?

Once installed, right-click your project in the Solution Explorer -> Add -> Connected Service. In the Connected Services window that appears, select OData Connected Service. Next, OData Connected Service provides us a wizard where we can configure settings for the service we want to connect to.

How do I connect to OData API?

Connect to the OData Sample ServiceGo to the main menu in Dundas BI and create a new data connector. Click the Name box and enter a name for this connector (e.g., OData Sample Service). Set the Data Provider dropdown to OData. Select the Authentication method.


2 Answers

The documentation of client side API seems to be a bit neglected by Olingo. But there is an example in the GIT repository at samples / client.

Basically for a GET you do the following:

String serviceUrl = "http://localhost:9080/odata-server-sample/cars.svc"
String entitySetName = "Manufacturers";

ODataClient client = ODataClientFactory.getClient();
URI absoluteUri = client.newURIBuilder(serviceUri).appendEntitySetSegment(entitySetName).build();
ODataEntitySetIteratorRequest<ClientEntitySet, ClientEntity> request = 
client.getRetrieveRequestFactory().getEntitySetIteratorRequest(absoluteUri);
// odata4 sample/server limitation not handling metadata=full
request.setAccept("application/json;odata.metadata=minimal");
ODataRetrieveResponse<ClientEntitySetIterator<ClientEntitySet, ClientEntity>> response = request.execute(); 
ClientEntitySetIterator<ClientEntitySet, ClientEntity> iterator = response.getBody();

while (iterator.hasNext()) {
     ClientEntity ce = iterator.next();
     System.out.println("Manufacturer name: " + ce.getProperty("Name").getPrimitiveValue());
}

Have a look at the sample in the Olingo code base to get further details how to retrieve metadata, process all properties etc.

To do a POST you can do as follows. (Note this is not tested code and the sample Car service is read-only.) First you build up data as ClientEntity. You do e.g. with

ClientComplexValue manufacturer = of.newComplexValue("Manufacturer");
manufacturer.add(of.newPrimitiveProperty("Name", of.newPrimitiveValueBuilder().buildString("Ford")));

Then you send the POST request

ODataEntityCreateRequest<ClientEntity> request = client.getCUDRequestFactory().getEntityCreateRequest(absoluteUri, manufacturer);
ODataEntityCreateResponse<ClientEntity> response = request.execute();

So this is not with POJO classes - the result type is ClientEntity, which presents the data as name/value maps. There is already another unanswered question about that particular topic at Olingo - Create strongly typed POJOs for client library of OData service and I suggest that we turn there to follow up on that.

like image 75
Torsten Küpper Avatar answered Sep 30 '22 17:09

Torsten Küpper


For SDL OData framework, you can check this Github Test class on how to use OData client.

SDL OData framework is based on the EDM classes and a simple example to get all Products (Product Edm Entity) will look like

// Create and configure the client
DefaultODataClient client = new DefaultODataClient();
client.configure(componentsProvider);

//Build the query
ODataClientQuery query = new BasicODataClientQuery.Builder().withEntityType(Product.class).build();

//Execute the query
List<Object> entities = (List<Object>) client.getEntities(requestProperties, query);
like image 45
vinayknl Avatar answered Sep 30 '22 17:09

vinayknl