Getting started with REST

Introduction

REST, acronym for Representational State Transfer is coined by Roy Fielding to represent architectural style of networked systems. This is not a standard but an architectural style for an application that is based on different standards like HTTP, XML, URL, MIME types.  JAX-RS specification, JSR 311 defines a set of java APIs for development of RESTful web services and RESTful java application.  Sun Jersey, JBoss RESTEasy, Apache CXF provides implementation for JSR 311. In this article we will look at the steps needed to setup Eclipse IDE for developing REST application using Jersey. We will also write our first HelloWorld service.

Step 1 : Downloading needed jars

The following jars needed to be available at compile time and runtime,

  • jersey-core*.jar
  • jersey-server*.jar
  • jsr311-api-1.1.jar
  • asm*.jar

All these jars can be downloaded in zip format at http://download.java.net/maven/2/com/sun/jersey/jersey-archive/

Note: Setup procedure is different for maven users and not covered in this blog.

Step 2 : Creating a Dynamic Web Project

Follow the step 2 mentioned in https://veechand.wordpress.com/2009/09/16/4-easy-steps-to-load-strut2inaction-source/

Note: Have the project name as PracticeREST instead of Struts2InAction

Step 3 : Configuring Build Path

Right click on the project. Choose properties and then Java Build Path > Libraries. Using “Add External Jars” option add the four jars downloaded in step 1

Figure 1 : Adding REST Jars

Figure 1 : Adding REST Jars

Also, change the Default output folder to classes directory (create newly) under WEB-INF folder. This has to be done in Source tab as shown below

Figure 2 - Changing Default output folder

Figure 2 - Changing Default output folder

Step 4 : Configuring REST servlets

To load Jersey servlets dispatcher add the following entries to PracticeREST/WebContent/WEB-INF/web.xml

<servlet>
   <servlet-name>Jersey Web Application</servlet-name>
  <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
</servlet>
<servlet-mapping>
 <servlet-name>Jersey Web Application</servlet-name>
 <url-pattern>/myrest/*</url-pattern>
</servlet-mapping>
</servlet>

Now all our REST web services will be available through …/rest/[resource]

Step 5 : Writing our first “Greetings” service

Create a new class my.practice.rest.Greetings and paste the below code


package my.practice.rest;
import javax.annotation.Resource;
import javax.ws.rs.DefaultValue;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.QueryParam;
/*
* @author veechand
*/
@Path("/sayhello/")
public class Greetings {
@GET
public String sayHello(@QueryParam("name") @DefaultValue ("NoName") String name ){
return "Howdy "+name+" !! Welcome to Jersey REST.";
}
}

The meaning of the annotations used in the above code are as below

 

Annotation Description Values
@Path Identifies the URI path that serves a request. This corresponds to javax.ws.rs.Path package Our Greeting service will be available through http://hostaddress:port-number/PracticeREST/rest/sayhello
@Get Identifies the method that serves the Get request This corresponds to javax.ws.rs.Get package
@QueryParam Maps between the URL parameter and a java variable. This corresponds to javax.ws.rs.QueryParam package. In our case the “name” parameter in URL is mapped to java variable, name
@DefaultValue Defines the default value. This value will be assigned to the variable if URL doesn’t have the specified parameter. In our case NoName will be assigned by default.

Step 6 : Testing our Greetings Service

You will be able to access the Greetings service by launching   http://hostaddress:port-number/PracticeREST/rest/sayhello?name=veechand in your favorite http client

Note: You have to replace hostaddress and port-number with corresponding values.

Figure 3 - Result

Figure 3 - Result

Closing Notes:

List of principles of a REST web design can be read at http://www.xfront.com/REST-Web-Services.html. Yes, we are violating the 2nd principle. I leave fixing this violation to readers. Comment if you have any issues in getting through it.

Keep watching for more write ups on Jersey REST

Add to FacebookAdd to NewsvineAdd to DiggAdd to Del.icio.usAdd to StumbleuponAdd to RedditAdd to BlinklistAdd to TwitterAdd to TechnoratiAdd to Furl

Advertisements

2 Responses

  1. […] with REST annotations Posted on November 9, 2009 by Veerabahu In the last article in addition to setting up our IDE for writing REST services we have also got friendly with four […]

  2. Good article to start the Jersey 🙂

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: