Creating a sample RESTful web service

In this section, we will create a RESTful web service. Right-click on the JAX-RS Web Services node in Project Explorer and select New JAX-RS Resource, as shown here:

Creating a sample RESTful web service

Select the jboss-jaxrs project and specify Package as org.jboss.jaxrs.rest, Name as HelloWorldResource, Resource path as /helloworld, and click on Next, as shown here:

Creating a sample RESTful web service

In the JAX-RS Application window, select the Source folder as jboss-jaxrs/src/main/java and specify Package as org.jboss.jaxrs.rest, Name as HelloRESTApplication, Application path as /rest, and click on Finish, as shown here:

Creating a sample RESTful web service

The org.jboss.jaxrs.rest.HelloWorldResource class gets generated, as shown in the Project Explorer.

Creating a sample RESTful web service

A JAX-RS RESTful web service resource is defined using a root resource class. A web service's URI is constructed from path designators, which are specified using the @PATH annotation. A resource is defined using a root resource class. The root resource classes are POJOs annotated with @PATH with one or more class methods annotated with a resource method designator (@GET, @PUT, @POST, @DELETE) or with the @PATH annotation or with both a resource method designator and @PATH. The root class methods that are annotated with resource method designators are called resource methods. The resource methods that are annotated with @PATH are called subresource methods. The class methods that are annotated only with @PATH are called subresource locators.

The resources respond to HTTP methods such as GET, POST, PUT, and DELETE (http://www.w3.org/Protocols/HTTP/Methods.html). For example, a client may get a resource representation with GET, upload a modified copy of the resource using PUT, or delete the resource using DELETE. The resource methods (and the subresource methods and subresource locators) may return a resource representation in various formats such as HTML, plain text, XML, PDF, JPEG, and JSON.

We will create a root resource class with some resource methods using the @GET request method designator. Annotate the Java class org.jboss.jaxrs.rest.HelloWorldResource with the @PATH annotation. Specify the URI path on which the Java class will be hosted as /helloworld:

@Path("/helloworld")
public class HelloWorldResource {
…
}

Next, add resource methods to produce three different MIME types. Add the resource methods getClichedMessage(), getXMLMessage(), and getHTMLMessage() (method names are arbitrary) annotated with the @GET annotation, which indicates that the resource methods will process the HTTP GET requests from a client. Each of the resource method produces a different MIME type resource representation. Each of the resource methods returns String, but the MIME type returned and specified in the @Produces annotation is different for the different resource methods. We will output a "Hello JAX-RS" message in three different MIME types: text/plain, text/xml, and text/html. The String value returned in each of the resource methods is in the format corresponding to the designated MIME type. Add two versions of the getXMLMessage() method one for a JSP client, as discussed in a later section. Add a resource method annotated with @Produces("application/xml") to demonstrate the requirement for the produced MIME type to match an acceptable MIME type in a client. The root resource class is listed as follows; some sections have been commented out for testing the resource methods separately:

package org.jboss.jaxrs.rest;

import javax.ws.rs.GET;
import javax.ws.rs.Produces;
import javax.ws.rs.Path;
import javax.ws.rs.core.MediaType;

// The Java class will be hosted at the URI path "/helloworld"
@Path("/helloworld")
public class HelloWorldResource {

  // The Java method will process HTTP GET requests
  //
  // The Java method will produce content identified by the MIME Media
  // type "text/plain"
  @GET
  @Produces("text/plain")
  public String getClichedMessage() {
    // Return some cliched textual content
    return "Hello JAX-RS";
  }
  /**
   * @GET
   * @Produces("application/xml") public String getXMLMessage() { return
   *                              "<?xml version="1.0"?>" +
   *                              "<hello> Hello JAX-RS" + "</hello>"; }
  */

//for java  client
@GET
  @Produces("text/xml")
  public String getXMLMessage() { 
    return "<?xml version="1.0"?>" + "<hello> Hello JAX-RS" + "</hello>";
  } 

//for jsp client
  /**@GET
  @Produces("text/xml")
  public String getXMLMessage () {
    return "&lt;?xml version="1.0"?&gt;" + "&lt;hello&gt;Hello JAX-RS" + "&lt;/hello&gt;";
  }*/

  @GET
  @Produces("text/html")
  public String getHTMLMessage() {
    return "<html> " + "<title>" + "Hello JAX-RS" + "</title>"
        + "<body><h1>" + "Hello JAX-RS" + "</body></h1> " + "</html> ";
  }

}

We also need to create a web deployment descriptor web.xml. Select File | New | Other, and in New, select JBoss Tools Web | Web Descriptor and click on Next, as shown here:

Creating a sample RESTful web service

In New Web Descriptor File, select the /jboss-jaxrs/src/main/webapp/WEB-INF folder and specify Name as web.xml and select Version as 3.1, and click on Finish, as shown here:

Creating a sample RESTful web service

In the web.xml file, specify a servlet for the servlet class com.sun.jersey.spi.container.servlet.ServletContainer, which is a servlet for deploying root resource classes. Specify the servlet mapping URL pattern as /jaxrs/*:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
 <display-name>EclipseJAX-RS</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  <servlet>
    <servlet-name>JAX-RS Servlet</servlet-name>
    <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer </servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>JAX-RS Servlet</servlet-name>
    <url-pattern>/jaxrs/*</url-pattern>
  </servlet-mapping>
</web-app>

The web.xml file is shown in Project Explorer as follows:

Creating a sample RESTful web service
..................Content has been hidden....................

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