Java EE 6 supports Java API for RESTful Web Services (JAX-RS), a Representational State Transfer (RESTful) web service. This type of service does not require WDSL or XML messages. In this recipe we will create a simple RESTful application, RESTApplication
, which returns a random tip of the day message.
The beauty of this application is its simplicity and ease of development. We will create two EJBs: one for the application's functionality and a second one to represent the Web Service.
To create a JAX-RS application we need to:
The use of annotations makes both of these steps easy to do.
Create a new Web Service called RESTApplication
. In this application we will be adding two stateless session beans: TipSessionBean
and TipOfTheDay
.
Next, create a packt
package for the two stateless EJBs. Follow this by creating a stateless session bean, TipSessionBean
, to support an application returning a random tip of the day message. Next, we will create the Web Service to access and use the bean.
The TipSessionBean
is straight forward. The EJB uses an array of strings to hold the tips. The getTip
method randomly returns a tip from the array.
package packt; import javax.ejb.Stateless; @Stateless public class TipSessionBean { private final static String tips[] = { "I hear and I forget. I see and I remember. I do and I understand", "Study the past if you would define the future", " Life is simple, it's just not easy."}; public String getTip() { return tips[(int)(Math.random()*tips.length)]; } }
Now let's turn our attention to the RESTful application. A stateless session bean, called TipOfTheDay
, is used to represent the service.
@Path("tipoftheday") @Stateless public class TipOfTheDay { @EJB TipSessionBean tips;@GET @Produces("text/html")
public String processGet() { return getTip(); } @POST @Produces("text/html")
public String processPost() { return getTip(); } private String getTip() { return tips.getTip(); } }
The service can be tested using an IDE tester or by typing the URL of the service into a browser as illustrated in the following screenshot:
At the heart of the TipSessionBean
is the getTip
method. This method used the Math
class' random
method to return a double number greater than or equal to 0.0 but is less than 1.0. The return value was multiplied by the length of the array, three in this case. This returned a number between 0 and 2.999997. The result was cast to an integer (between 0 and 2 inclusive) and then used to select the tip.
public String getTip() { return tips[(int)(Math.random()*tips.length)]; }
In the TipOfTheDay
class, the @Path annotation used a string to specify the name of the service. This name is part of the URL used by a client. The class itself used the @EJB annotation to inject the TipSessionBean
for the field tips
.
The @GET annotation configured the processGet
method as the method to use when a HTTP GET request is made. The @Produces annotation specified the type of output as text/html
. While we did not actually generate any HTML tags, we could if needed.
Most web applications will support both the GET and POST operations. However, from a Java EE perspective there is little difference between them. This allows us to create one method which handles both requests in the same way.
The processGet
method calls a private getTip
method of the service. A third method, processPost
, was added to POST requests and also called the servlet's getTip
method. The getTip
method then used the Tip's
object to return a tip.
The service can be tested using an IDE tester or typing in the URL of the service into a browser.
The actual URL is dependent on the configuration of the application. Consider the URL:
http://localhost:8080/RESTApplication/resources/tipoftheday
The context path for the application is http://localhost:8080/RESTApplication
where RESTApplication
is the name of the project. The relative URL is /resources as specified in the @ApplicationPath annotation discussed next. The last part, tipoftheday, is specified in the @Path annotation.
When a RESTLess application is created in an IDE such as NetBeans, an ApplicationConfig
class is automatically generated. You may be prompted to choose between various options. Normally, the default option will suffice.
This class, shown below, defines the components used in the application and supplies any metadata needed for the application. The ApplicationConfig
used for the tip of the day application uses the @ApplicationPath annotation to specify the base URI for all of the resources of the application. Notice that annotations can also be written prefixed with the package name as we sometimes do with Java classes.
package org.netbeans.rest.application.config; @javax.ws.rs.ApplicationPath("resources") public class ApplicationConfig extends javax.ws.rs.core.Application { }
Notice in this example the application path is given as resources
. This is why the URL used to test the application uses this string in front of the resource name.
3.144.114.85