Olingo - 为OData服务的客户端库创建强类型POJO

时间:2014-06-30 00:48:19

标签: java odata olingo

我使用Apache Olingo作为Java SDK的OData客户端,我将为RESTful OData API提供。在SDK中,我希望能够使用强类型类来表示OData实体。我很难轻易实现这一点,因此我觉得我在这里错过了不同的策略。

Olingo的方式似乎是获得一个ODataClient对象,该对象为用户提供了一系列与API交互的有用方法。 ODataClient正在使用一堆工厂方法来构建我的请求。例如,这是我用来从Northwind示例OData服务获取Customers的代码。 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());
}

我希望最终得到迭代器中的强类型实体(即Customer customer = iterator.next())。但是,我不确定如何实际做到这一点。

如果我创建一个扩展Customer的{​​{1}}类并尝试执行诸如ODataEntity之类的转换,那么我得到一个Customer customer = (Customer) iterator.next(),因为迭代器中的对象只是ClassCastException对象,对ODataEntity子类一无所知。

我的下一个想法是引入泛型,但这样做需要对Olingo库进行大量修改,这让我觉得有更好的方法来做到这一点。

我使用Apache Olingo 4的开发版本,因为OData服务必须使用OData 4。

我错过了什么?

1 个答案:

答案 0 :(得分:5)

它并没有真正做广告,但现在在Olingo有一个POJO生成器,位于source tree的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>

在实验中,我将Olingo Car示例服务的EDM Metadata存储在src / main / resources / metadata.xml中。不知何故,该插件想要创建一个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());
}