java 调用odata,如何使用Olingo或SDL OData Framework在Java中使用OData4服务

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.

解决方案

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

}

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 request = client.getCUDRequestFactory().getEntityCreateRequest(absoluteUri, manufacturer);

ODataEntityCreateResponse 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.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值