Skip to main content

Exposing JPA Entities as RESTful Web Services in the Oracle Cloud

Posted by bleonard on August 30, 2013 at 9:13 AM PDT

I'm expanding on an earlier post where I explained how to deploy JAX-RS Web Services to the Oracle Cloud. In that entry the web service simply returned a hard-coded "Hello World". In reality, you most likely want your web service to expose something more meaningful. In this post I'm going to expand the HelloJerseyApp to display the results from a table, which I'll expose using a JPA entity.

Step 1. Open HelloJersey in JDeveloper

Download the HelloJerseyApp, which was created in the previous blog post, and open it in JDeveper.

Step 2. Create a JPA Entity

As I already have the Oracle HR sample database set up in the cloud, I will create an entity to represent the COUNTRIES table. Note, I also have a local copy of the HR sample database that I will initially use the generate the entity. I will switch the configuration to use the cloud datasource just before I deploy.

Open the New Gallery and select Entities from Tables:

Select EJB 3.0 - JPA Entities:

Select Next to accept the default Persistence Unit:

Select Online Database Connection as the Connection Type:

Create a new (or copy an existing) connection to the HR schema:

Select the COUNTRIES table:

Optionally set the Package Name:

And optionally change the entity name. I like to keep them singular:

And click Finish to generate the Entity.

Step 3: Generate a Java Service Facade

The Java Service Facade has a Main method, which will allow you to easily test your methods before deploying to the cloud.

Right-click the persistence.xml (that was generated when we created the entity) and select New Java Service Facade. I'm updating the Service Class Name to facade.CountryFacade.

For reasons beyond my understanding, this wizard does not recognize the persistence unit that was just created as part of the Create Entities from Tables wizard. No worries, we can have 2 persistence units.

Select the defaults provided to create a new persistence unit:

You can leave the Java Service Facade Method defaults:

And click Finish to generate the facade class.

Step 4: Add the Service Method

Here's a simple service method that will return an array of country names:

 public String[] getCountryList() {
   List<Country> allCountries = getCountryFindAll();
   String[] countryList = new String[allCountries.size()];
   for (int i = 0; i < allCountries.size(); i++) {
     countryList[i] = allCountries.get(i).getCountryName();
     System.out.println(countryList[i]);
   }
   return countryList;
}

With the System.out.println, you can then call this method from the Main method to quickly test the entity:

 public static void main(String [] args) {
   final CountryFacade countryFacade = new CountryFacade();
   //  TODO:  Call methods on countryFacade here...
   countryFacade.getCountryList();
}

Right-click the source and select Run. Look for the results in the Output window:

Step 5: Rest Enable the Class and Method

Add the @Path annotation to the class:

  import javax.ws.rs.Path;
  @Path("/country")
  public class CountryFacade {

At the @Get, @Path and @Produces annotations to the method and delete the System.out.println:

  @GET
  @Path("/countries")
  @Produces(MediaType.APPLICATION_JSON)
  public String[] getCountryList() {
    List<Country> allCountries = getCountryFindAll();
    String[] countryList = new String[allCountries.size()];
    for (int i = 0; i < allCountries.size(); i++) {
      countryList[i] = allCountries.get(i).getCountryName();
    }

Step 6: Update Datasource for Oracle Cloud

Now that the service method is behaving as we like, we need to update the persistence unit's datasource to the Oracle Cloud.

Remember when we ran the New Java Service Facade wizard, a new persistence unit was created, HelloJerseyProj-1. We are going to switch the CountryFacade class to use the original persistence unit, HelloJerseyProj.

 @Path("/country")
public class CountryFacade {
private EntityManagerFactory emf = Persistence.createEntityManagerFactory("HelloJerseyProj");

We will then update the HelloJerseyProj persistence unit to call the cloud datasource, which is database.

 <persistence-unit name="HelloJerseyProj">
    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
    <jta-data-source>javatrial0129db</jta-data-source>
    <class>entity.Country</class>
    <properties>
      <property name="eclipselink.target-server" value="WebLogic_10"/>
      <property name="javax.persistence.jtaDataSource"
                value="javatrial0129db"/>
    </properties>
  </persistence-unit>

Step 7: Deploy and Use

Deploy as before. Once deployed, you'll be able to access the list of countries at the following URL (swapping out my Identity Domain for yours):

https://javatrial0129-usoracletrial80797.java.us2.oraclecloudapps.com/HelloJersey/jersey/country/countries

AttachmentSize
HelloJerseyApp.zip195.18 KB