11

RESTful OData API に提供する Java SDK の OData クライアントとして Apache Olingo を使用しています。SDK では、OData エンティティを表す厳密に型指定されたクラスを使用できるようにしたいと考えています。これを簡単に実装するのに苦労しているため、ここで別の戦略が欠けているように感じます。

Olingo の方法はODataClient、API とやり取りするための便利なメソッドをユーザーに提供するオブジェクトを取得することのようです。私のODataClientリクエストを構築するために、一連のファクトリメソッドを使用しています。たとえば、これはCustomersNorthwind サンプル 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());
}

イテレータ (すなわち ) から強く型付けされたエンティティで終わりたいと思いますCustomer customer = iterator.next()。ただし、実際にそれを行う方法はわかりません。

拡張して次のようなキャストを実行しようとするCustomerクラスを作成すると、イテレータ内のオブジェクトは単なるオブジェクトであり、サブクラスについて何も知らないため、が得られます。ODataEntityCustomer customer = (Customer) iterator.next()ClassCastExceptionODataEntityCustomer

次に考えたのはジェネリックを導入することでしたが、そうすると Olingo ライブラリにかなりの量の変更が必要になり、これを行うためのより良い方法があると思いました。

OData サービスは OData 4 を使用する必要があるため、Apache Olingo 4 の開発バージョンを使用しています。

私は何が欠けていますか?

4

1 に答える 1

7

あまり宣伝されていませんが、現在、Olingo の ext / pojogen-maven-pluginのソース ツリーに POJO ジェネレーターがあります。残念なことに、POJO を使用するために、別のプログラミング モデルを持つ別のレイヤーが追加されます。これは、メモリにキャッシュされたエンティティを保持し、フラッシュ操作で OData サービスと同期します。Olingos リクエスト ファクトリに基づく、より従来型のリクエスト/レスポンス モデルに適応させることに本当に興味があります。

ただし、試してみることはできます。pom に pojogen-maven-plugin と odata-client-proxy を含めます。POJO 生成は、pom でトリガーできます。

    <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 メタデータを src/main/resources/metadata.xml に保存しました。どういうわけか、プラグインは 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());
}
于 2016-02-14T22:41:42.317 に答える