12 - Building EJB Based RESTful Web Services

Building EJB Based RESTful Web Services

Now for building Restful web service from an EJB class, all that we need to do is to package the web service in a Web application (servlet 3.0/3.1) and create an application class that loads the EJB class as a Rest Root class and annotating the EJB class with @Path annotation.

In the following example, we will build a Dynamic web project that exposes an EJB class as a Restful web services using the JAX-RS APIs.

       

Create a new Dynamic Web Project in Eclipse IDE

 

       

Name the project as HelloWorldJAXRS and set the module version to 3.1, now we will create an Application class that defines the Rest web service Roots in our application.

         

Create a new class called MyRestApplication. Set the package as name com.ejb3.webservice and set the class Superclass as javax.ws.rs.core.Application and click the finish button.

An empty class that extends the Application class will be generated.

Now create a new stateless EJB class as we have done in the previous section, but this time the class will be included in the war file and not in the ejb jar file, as in JEE 7 it is allowed to declare EJB classes in war files.

Name the new stateless beans as HelloJAXRSWebService and set its package as com.ejb3.webservice.

Till now the project should have the following:

         

Now edit the EJB class file

package com.ejb3.webservice;

import javax.ejb.LocalBean;
import javax.ejb.Stateless;

/**
 * Session Bean implementation class HelloJAXRSWebService
 */
@Stateless
@LocalBean
public class HelloJAXRSWebService {

   /**
     * Default constructor. 
     */
    public HelloJAXRSWebService() {
        // TODO Auto-generated constructor stub
    }
}

And convert it to be as follows:

package com.ejb3.webservice;

import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;

/**
 * Session Bean implementation class HelloJAXRSWebService
 */
@Stateless
@LocalBean
@Path("/hellows")
public class HelloJAXRSWebService {

   /**
     * Default constructor. 
     */
    public HelloJAXRSWebService() {
        // TODO Auto-generated constructor stub
    }

    @GET
    @Produces("text/plain")
    public String sayHello() {
        return "Hello";
    }
}

The extra lines added to the EJB class are as follows:

@Path: declares that this EJB should be exposed as restful web service and will listen to path /hellows, the path will be relative to the restfull application path.

@GET: means that the method will be invoked on the HTTP GET request.

@Produces: means that the method will return data of type plain text.

Now edit the MyRestApplication class:

package com.ejb3.webservice;

import javax.ws.rs.core.Application;

public class MyRestApplication extends Application {

}

And convert it to the following:

package com.ejb3.webservice;

import java.util.HashSet;
import java.util.Set;

import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;

@ApplicationPath("/rest")
public class MyRestApplication extends Application {
    @Override
    public Set<Class<?>> getClasses() {
        Set<Class<?>> classes = new HashSet<Class<?>>();
        classes.add(HelloJAXRSWebService.class);
        return classes;
    }
}

The annotation means that our restfull web services URLs will be relative to the URL web context/rest/web service url.

Now let us convert our web application configuration to a JAX-RS application using eclipse IDE, this is just for better IDE support and has no effect on the running behavior of the application.

From the Configure menu, select Add JAX-RS 1.1 support.

A new icon will appear in the package explorer tree called JAX-RS Web Services.

                     

Now right click on the GET /rest/hellows icon and select to debug on Server.

          

    Choose our configured server

     

  And click finish button to start the server, a new window will appear called Web Service Tester.

    

You can notice the URL in the top bar of the window:

http://localhost:8080/HelloWorldJAXRS/rest/hellows, this is the URL that our web service listens t. Click on the play icon on the top bar to invoke the web service, in the response window, a Hello word will be printed.

   

You can also view the same response by invoking the web service URL from your browser directly.

The web service can be modified to support all Restful features like path parameters and form parameters. The web service  will support all EJB features like injection, lifecycle, transaction management…etc.

Like us on Facebook