Home > Articles > Programming > Java

  • Print
  • + Share This
Like this article? We recommend

Like this article? We recommend

Coding a REST Service

Coding a REST Service

REST is most appropriate in HTTP-centric situations where the additional capabilities of SOAP-based interactions don't justify the additional overhead and complexity that SOAP introduces. If you need a very secure service that spans multiple endpoints (at different locations), then you would likely use SOAP with XML as it supports the WS Security specifications which guarantee message integrity, encryption, confidentiality, end-to-end authentication persistence using security tokens, and more.

A better and more common approach is to use JavaScript Object Notation (JSON) when parsing and constructing data invocations between the service consumer and producer. JSON is much more intuitive than SOAP and XML, making it an ideal choice for RESTful web services. You can learn more about JSON at http://json.org/.

Step 1: Download a Reference Implementation to JAX-RS

To get started, you need to download a reference implementation of JAX-RS provided by the GlassFish Project. Jersey provides a servlet that analyzes the incoming HTTP request and selects the correct class and method to respond to this request. This selection is based on annotation in the class and methods.

You can download Jersey zip file(core dependencies) from http://jersey.java.net/.

Step 2: Create Your Java Project

Using Eclipse, NetBeans, Maven, or whatever you like, create the project's directory structure. I'm calling the project jaxrshello.

Next, copy all the Jersey core-dependency jars into the WEB-INF/lib folder of the project.

Step 3: Register the Jersey Reference Implementation Servlet with the Project

In order to get Jersey to intercept the requests, you need to modify the Web.xml file as in the following:

<?xml version="1.0" encoding="UTF-8"?> 
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/
 xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5"> 
<display-name>jaxrshello</display-name> <servlet> 
<servlet-name>Jersey REST Service</servlet-name> 
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class> 
<init-param> <param-name>com.sun.jersey.config.property.packages</param-name> 
<param-value>response</param-value> </init-param> 
<load-on-startup>1</load-on-startup> </servlet> 
<servlet-mapping> <servlet-name>Jersey REST Service</servlet-name> 
<url-pattern>/rest/*</url-pattern> </servlet-mapping> </web-app> 

The parameter com.sunjersey.config.property.packages should have the package where the web service classes can be found. In this example, the response package will contain these classes.

Step 3: Create a Class That Registers Itself with the HTTP GET Request

Whenever a GET method is processed by the Jersey Servlet with a request the class path /hello, this class will be called and will return a response based on the content type:

package response;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
@Path("/hello")
public class Hello {
    // This method is called if TEXT_PLAIN is request
    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public String sayPlainTextHello() {
        return "Hello World from JAX-RS";
    }
    // This method is called if XML is request
    @GET
    @Produces(MediaType.TEXT_XML)
    public String sayXMLHello() {
        return "<?xml version=\"1.0\"?>" + "<hello>Hello World from JAX-RS" + "</hello>";
    }
    // This method is called if HTML is request
    @GET
    @Produces(MediaType.TEXT_HTML)
    public String sayHtmlHello() {
        return "<html> " + "<title>" + "Hello World from JAX-RS" + "</title>"
                + "<body><h1>" + "Hello World from JAX-RS" + "</body></h1>" + "</html> ";
    }
}

The @Path annotation at the top of the class tells Jersey that this class will be called when the URI request contains the /hello action.

The @Get annotation tells Jersey to map these methods of the class to the HTTP GET method. The @Produces annotation tells the servlet to respond with the correct content type based on the request.

Step 4: Test the Service

To test the service, browse to the URL hosting the "RESTlet":

http://localhost:8080/jaxrshello/rest/hello.

Because the content type of our Request header was for plain text, the service returns Hello World from JAX-RS to the screen.

  • + Share This
  • 🔖 Save To Your Account