Transforming the JPA model into OData-enabled RESTful web services

Open Data Protocol (OData) is an open protocol for the web. We discussed the advantages of using OData with RESTful web APIs in the Using Open Data Protocol (OData) with RESTful web APIs section in Chapter 9, The Role of RESTful APIs in Emerging Technologies. In this section, we will see how to build the OData services for the Java Persistence API (JPA) model.

Apache Olingo is an open source Java library that implements the OData protocol. You can use the Apache Olingo framework to enable OData services for your JPA model.

With the Olingo framework, you can easily transform your JPA models into OData services using the OData JPA Processor Library.

The high-level steps for enabling the OData 2 services for your JPA model are listed as follows:

  1. Build a web project to hold the RESTful web API components.
  2. Generate the JPA entities as appropriate.
  3. The next step is to configure the application to use the Apache Olingo framework for generating the OData services for the JPA model.
  4. Add a dependency to the Olingo OData Library (Java) and the OData JPA Processor Library. The complete list of the JAR files is listed at http://olingo.apache.org/doc/odata2/tutorials/CreateWebApp.html.
  1. Add a service factory implementation, which provides the means for initializing the OData JPA Processors and the data model provider (JPA entity). You can do this by adding a class that extends org.apache.olingo.odata2.jpa.processor.api.ODataJPAServiceFactory, as shown in the following:
//Imports are removed for brevity 
public class ODataJPAServiceFactoryImpl extends   
    ODataJPAServiceFactory { 
   //HR-PU is the persistence unit  
   //configured in persistence.xml for the JPA model 
      final String PUNIT_NAME = "HR-PU"; 
   @Override 
   public ODataJPAContext initializeODataJPAContext()  
      throws ODataJPARuntimeException { 
     ODataJPAContext oDataJPAContext =  
          getODataJPAContext(); 
     oDataJPAContext.setEntityManagerFactory( 
     JPAEntityManagerFactory 
    .getEntityManagerFactory(PUNIT_NAME)); 
      oDataJPAContext.setPersistenceUnitName(PUNIT_NAME); 
  return oDataJPAContext; 
   } 
  //Other methods are removed for brevity 
} 
  1. Configure the web application by adding CXFNonSpringJaxrsServlet to web.xml. This servlet manages the OData features for your JPA model. Specify the service factory implementation class that you created in the last step as one of the init parameters for this servlet. The web.xml configuration may look like the following lines:
<servlet> 
    <servlet-name>ODataEnabledJPAServlet</servlet-name> 
    <servlet-class> 
      org.apache.cxf.jaxrs.servlet.CXFNonSpringJaxrsServlet 
    </servlet-class> 
    <init-param> 
      <param-name>javax.ws.rs.Application</param-name> 
      <param-value> 
        org.apache.olingo.odata2.core.rest.app.ODataApplication 
      </param-value> 
    </init-param> 
    <init-param> 
      <param-name>org.apache.olingo.odata2.service.factory</param-name> 
      <param-value>com.packtpub.odata.ODataJPAServiceFactoryImpl</param-value> 
    </init-param> 
    <load-on-startup>1</load-on-startup> 
</servlet> 
<servlet-mapping> 
  <servlet-name>ODataEnabledJPAServlet</servlet-name> 
  <url-pattern>/odata/*</url-pattern> 
</servlet-mapping> 

Now you can build the application and deploy it to a JAX-RS container, such as the GlassFish server. The Olingo framework will automatically convert all the JPA entities into the OData services. You can access the APIs via standard OData clients.

To test the deployment, try accessing the OData service as follows:
http://localhost:8080/<appname>/odata

You can go through the tutorial to learn how to build a Java client for OData services at https://olingo.apache.org/doc/odata2/tutorials/OlingoV2BasicClientSample.html.

The complete source code for this example is available on the Packt website. You can download the example from the Packt website link that we mentioned at the beginning of this book, in the Preface. In the downloaded source code, see the rest-chapter9-odata/rest-chapter9-odata-service project.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.117.231.15