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

后端 未结 1 1788
被撕碎了的回忆
被撕碎了的回忆 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

        
            org.codehaus.mojo
            build-helper-maven-plugin
            1.8
            
                
                    process-sources
                    
                        add-source
                    
                    
                        
                            ${project.build.directory}/generated-sources
                        
                    
                
            
        
    
        
            org.apache.olingo
            pojogen-maven-plugin
            4.2.0-SNAPSHOT
            
                ${project.build.directory}/generated-sources
                ${basedir}/src/main/resources/metadata.xml
                odata.test.pojo
            
            
                
                    v4pojoGen
                    generate-sources
                    
                        v4pojoGen
                    
                
            
        
    
    

    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 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)
提交回复
热议问题