How can I use the OData4 service in Java using Olingo or SDL OData Framework

I need to use the OData4 service from Java and based on the list of frameworks on the OData website , two options: Olingo or SDL Odata Framework . My problem is that the documentation for both of these projects is focused on writing a service that does not consume it. The Olingo site is linking to a blog post from 2014 that does not support the API with the current version, and I could not find anything on the github SDL pages.

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

My limited understanding is that OData moves any information about the actual object model from compile time to the client runtime. I gladly ignore this and encode with a fixed object model, because the service we use will not change.

+5
source share
2 answers

Client-side API documentation seems to be ignored by Olingo. But in the sample / client there is an example in the GIT repository .

Basically for 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()); } 

Take a look at the sample in the Olingo code base for more information on how to get metadata, handle all properties, etc.

To do a POST, you can do the following. (Note that this is not verified code, and the Car service sample is read-only.) First, you create the data as ClientEntity. You for example with

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

Then you send a 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 represents the data as name / value maps. There is already another unresolved question about this specific topic in Olingo - the creation of strongly typed POJOs for the OData client service library and I suggest that we go there to follow up on this.

+6
source

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

The OData SDL structure is based on EDM classes and a simple example for getting 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); 
0
source

Source: https://habr.com/ru/post/1242636/


All Articles