Olingo - Create strongly typed POJOs for client library of OData service

不羁岁月 提交于 2019-11-30 19:27:46
Torsten Küpper

It is not really advertised, but there is nowadays a POJO generator in Olingo, in the source tree at ext / pojogen-maven-plugin. Unfortunately for using the POJOs another layer with a different programming model is added, which holds entities cached in memory and syncs with OData service on a flush operation. I would be really interested in adapting it to a more conventional request/response model based on Olingos Request Factories.

However, you could try it out. In your pom include pojogen-maven-plugin and odata-client-proxy. The POJO generation can be triggered in the pom with

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

For the experiment I stored the EDM Metadataof the Olingo Car example service at src/main/resources/metadata.xml. Somehow the plugin wants to create an inbetween ojc-plugin folder and I just moved the generated Java code at the proper place manually.

At that point you have a Service.java and Java interfaces for each entity or complex type in the EDM model.

You can make use of it to read some entities like this

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());
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!