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

后端 未结 1 1794
被撕碎了的回忆
被撕碎了的回忆 2021-01-04 18:15

I\'m using Apache Olingo as an OData client for a Java SDK that I will provide for a RESTful OData API. In the SDK I want to be able to have strongly typed classes to repres

相关标签:
1条回答
  • 2021-01-04 18:56

    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());
    }
    
    0 讨论(0)
提交回复
热议问题