如何使用Olingo或SDL OData Framework在Java中使用OData4服务 [英] How can I consume an OData4 service in Java using either Olingo or the SDL OData Framework

查看:585
本文介绍了如何使用Olingo或SDL OData Framework在Java中使用OData4服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用Java中的OData4服务并基于 OData网站上的框架列表这两个选项是 Olingo SDL Odata Framework 。我的问题是这两个项目的文档都侧重于编写一个不消耗一个服务的服务。 Olingo网站链接到2014年的博客文章,该文章与当前版本不兼容API,我在SDL github页面上找不到任何内容。

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.

如果有人可以只是给我一个简单的POST / GET示例,使用一个很好的POJO对象模型。

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

我有限的理解是OData移动有关实际对象的任何信息模型从编译时到客户端上的运行时。我很乐意忽略这个和针对固定对象模型的代码,因为我们使用的服务不会改变。

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.

推荐答案

客户端API的文档似乎有点被Olingo忽略了。
但是 GIT中有一个例子样本/客户端的存储库

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.

基本上对于GET,您可以执行以下操作:

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

查看Olingo代码库中的示例以获取更多详细信息
检索元数据,处理所有属性等。

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

要执行POST,您可以执行以下操作。 (请注意,这不是经过测试的代码,示例Car服务是只读的。)
首先,您将数据构建为ClientEntity。你这样做with

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")));

然后你发送POST请求

Then you send the POST request

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

所以这不适用于POJO类 - 结果类型是ClientEntity,它将数据显示为名称/价值图。
关于该特定主题的另一个未回答的问题是
Olingo - 为OData服务的客户端库创建强类型POJO
我建议我们转到那里跟进。

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.

这篇关于如何使用Olingo或SDL OData Framework在Java中使用OData4服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆