我正在使用Apache Olingo作为Java SDK的OData客户端,我将为RESTful OData API提供.在SDK中,我希望能够使用强类型类来表示OData实体.我很难轻松实现这一点,因此我觉得我在这里错过了一个不同的策略.
Olingo的方式似乎是获取一个ODataClient对象,该对象为用户提供了许多与API交互的有用方法.该ODataClient用一堆工厂方法来构建我的要求.例如,这是我用来Customers从Northwind示例OData服务获取的代码.client是必要ODataClient类的一个实例.
String serviceRoot = "http://services.odata.org/V4/Northwind/Northwind.svc";
URI customersUri = client.newURIBuilder(serviceRoot)
.appendEntitySetSegment("Customers").build();
ODataRetrieveResponse<ODataEntitySetIterator<ODataEntitySet, ODataEntity>> response =
client.getRetrieveRequestFactory().getEntitySetIteratorRequest(customersUri).execute();
if (response.getStatusCode() >= 400) {
log("Error");
return;
}
ODataEntitySetIterator<ODataEntitySet, ODataEntity> iterator = response.getBody();
while (iterator.hasNext()) {
ODataEntity customer = iterator.next();
log(customer.getId().toString());
}
Run Code Online (Sandbox Code Playgroud)
我想从迭代器(即Customer customer = iterator.next())中得到一个强类型实体.但是,我不确定如何实际做到这一点.
如果我创建一个Customer扩展ODataEntity并尝试执行转换的类,Customer customer = (Customer) iterator.next()那么我得到一个,ClassCastException因为迭代器中的ODataEntity对象只是对象而Customer对子类一无所知.
我的下一个想法是引入泛型,但这样做需要对Olingo库进行大量修改,这让我觉得有更好的方法来做到这一点.
我正在使用Apache Olingo 4的开发版本,因为OData服务必须使用OData 4.
我错过了什么?
小智 5
它并没有真正做广告,但现在有一个POJO生成器在Olingo,在ext/pojogen-maven-plugin 的源代码树中.不幸的是,对于使用POJO,添加了具有不同编程模型的另一层,其保持在内存中缓存的实体并在刷新操作上与OData服务同步.我真的很想将它改编为基于Olingos Request Factories的更传统的请求/响应模型.
但是,你可以尝试一下.在您的pom中包括pojogen-maven-plugin和odata-client-proxy.可以在pom中触发POJO生成
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.8</version>
<executions>
<execution>
<phase>process-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>${project.build.directory}/generated-sources</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.olingo</groupId>
<artifactId>pojogen-maven-plugin</artifactId>
<version>4.2.0-SNAPSHOT</version>
<configuration>
<outputDirectory>${project.build.directory}/generated-sources</outputDirectory>
<localEdm>${basedir}/src/main/resources/metadata.xml</localEdm>
<basePackage>odata.test.pojo</basePackage>
</configuration>
<executions>
<execution>
<id>v4pojoGen</id>
<phase>generate-sources</phase>
<goals>
<goal>v4pojoGen</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
Run Code Online (Sandbox Code Playgroud)
对于实验,我在src/main/resources/metadata.xml存储了Olingo Car示例服务的EDM Metadata.不知何故,该插件想要创建一个inbetween ojc-plugin文件夹,我只是手动将生成的Java代码移动到适当的位置.
此时,您在EDM模型中为每个实体或复杂类型提供了Service.java和Java接口.
你可以利用它来读取这样的实体
Service<EdmEnabledODataClient> service = odata.test.pojo.Service.getV4("http://localhost:9080/odata-server-sample/cars.svc");
Container container = service.getEntityContainer(Container.class);
for (Manufacturer m : container.getManufacturers()) {
System.out.println(m.getName());
}
Run Code Online (Sandbox Code Playgroud)