如何使用Olingo或SDL OData Framework在Java中使用OData4服务

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

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

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

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

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

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 request = client.getRetrieveRequestFactory().getEntitySetIteratorRequest(absoluteUri); // odata4 sample/server limitation not handling metadata=full request.setAccept("application/json;odata.metadata=minimal"); ODataRetrieveResponse> response = request.execute(); ClientEntitySetIterator iterator = response.getBody(); while (iterator.hasNext()) { ClientEntity ce = iterator.next(); System.out.println("Manufacturer name: " + ce.getProperty("Name").getPrimitiveValue()); } 

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

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

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

然后发送POST请求

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

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

对于SDL OData框架 ,您可以检查此Github Test类如何使用OData客户端。

SDL OData框架基于EDM类,一个简单的例子可以让所有产品(Product Edm Entity)看起来像

 // 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 entities = (List) client.getEntities(requestProperties, query);