Goal
I spent some time putting all these pieces together configuring Jersey on Tomcat, so hope this write-up may help somebody who intends to do the same.
Quick Background
JAX-RS is an annotation-based API for implementing RESTful web services. Jersey is the JAX-RS reference implementation from Sun. Jersey website says that it is more than the reference implementation; Jersey provides an API so that the developers may extend it suit their needs. There are other implementations by different vendors: RESTEasy (from JBoss), Restlet, Apache CXF.
Prerequisites
- Download and install Tomcat (using version 6.0).
- Download Jersey (using Jersey 1.0.2, most recent at the time of writing this)
- I'm using Eclipse as IDE (heard that NetBeans has better REST support than Eclipse, I haven't tried it yet)
Getting Started
1. Create a new dynamic web project in Eclipse
2. Copy Jersey jars to the web project's library:
asm-3.1.jar, jersey-core.jar, jersey-server.jar, jsr-311-api-1.0.jar
3. Deployment descriptor
Add Jersey Servlet declaration to the web.xml of the web application (this step took a little while to figure out exact class name of the jersey implementation)
<servlet> <servlet-name>JerseyTest</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>JerseyTest</servlet-name> <url-pattern>/*</url-pattern> </servlet-mapping>
4.Resource class
Create a resource class, HelloWorldResource. See the power of JAX-RS and Jersey in the annotations below
package com.suryasuravarapu.jersey;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
@Path ("/helloworld")
public class HelloWorldResource {
@GET
@Produces ("text/plain")
public String sayHello() {
return "Hello World";
}
}
- Line 7: @Path indicates what URI path is mapped to the resource.
- Line 11:@GETindicates what HTTP method is allowed for this resource.
-Line 12: @Produces indicates what MIME type is returned by this resource.
5. Test it
Access the resource using http://localhost:8080/RestExample/helloworld (assuming your Tomcat is running on port 8080, otherwise adjust accordingly), you should see a response back with text 'Hello World'.
This is obviously a simple example but the focus here is mainly on the setup. Stay tuned, more RESTful stuff to come ...


Follow on Twitter
Surya Suravarapu Reply:
February 22nd, 2009 at 8:31 am
Another option is to use: MyEclipse. Although not free (but relatively inexpensive) brings a whole lot of useful features over Eclipse including JAX-RS support. But yes, NetBeans is one step ahead in the game, at this point.
[Reply]
Raj Reply:
February 4th, 2011 at 6:46 am
Hi Surya,
thanks for your post but i need some more help ,can u tell me how to call jax-rs from the jsp pages .i am using jquery ajax but i think there is some problem in cross domain call .can u tell me the way how to call from the jsp pages. please reply on rahulojha2011@gmail.com. thanks……..
[Reply]