我需要使用来自Java的OData4服务,根据OData website上的框架列表,有两个选择:Olingo或SDL Odata Framework。我的问题是,这两个项目的文档都专注于编写服务,而不是消费服务。Olingo站点链接到2014年的一篇博客文章,该文章与当前版本的API不兼容,我在SDL github页面上找不到任何东西。
如果有人能为我提供一个简单的POST / GET示例,并使用适当的POJO对象模型,那就太好了。
我有限的理解是,OData将有关实际对象模型的任何信息从编译时移动到客户端的运行时。我很高兴忽略这一点,并针对固定的对象模型进行编码,因为我们使用的服务不会改变。
发布于 2016-02-15 06:12:53
客户端API的文档似乎被Olingo忽略了一点。但在samples / GIT repository中有一个示例。
基本上,对于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<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代码库中的示例,以获得如何检索元数据、处理所有属性等的详细信息。
要发布帖子,您可以执行以下操作。(请注意,这不是经过测试的代码,示例汽车服务是只读的。)首先,将数据构建为ClientEntity。你可以这样做,例如用
ClientComplexValue manufacturer = of.newComplexValue("Manufacturer");
manufacturer.add(of.newPrimitiveProperty("Name", of.newPrimitiveValueBuilder().buildString("Ford")));然后发送POST请求
ODataEntityCreateRequest<ClientEntity> request = client.getCUDRequestFactory().getEntityCreateRequest(absoluteUri, manufacturer);
ODataEntityCreateResponse<ClientEntity> response = request.execute();所以POJO类不是这样的--结果类型是ClientEntity,它将数据表示为名称/值映射。在Olingo - Create strongly typed POJOs for client library of OData service上已经有另一个关于这个特定主题的悬而未决的问题,我建议我们转到那里跟进这个问题。
发布于 2016-05-04 17:05:48
对于SDL OData framework,您可以查看此Github Test class以了解如何使用OData客户端。
SDL框架是基于OData类和一个简单的例子来获得所有的产品(产品EDM实体)将如下所示
// 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);https://stackoverflow.com/questions/35297844
复制相似问题