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
#1 by jayanth on June 14, 2010 - 6:42 am
Quote
I am getting the following problem when i run the code
.sun.jersey.api.container.ContainerException: The ResourceConfig instance does not contain any root resource classes.
at com.sun.jersey.server.impl.application.WebApplicationImpl.processRootResources(WebApplicationImpl.java:1070)
at com.sun.jersey.server.impl.application.WebApplicationImpl.initiate(WebApplicationImpl.java:918)
at com.sun.jersey.server.impl.application.WebApplicationImpl.initiate(WebApplicationImpl.java:589)
at com.sun.jersey.spi.container.servlet.ServletContainer.initiate(ServletContainer.java:429)
at com.sun.jersey.spi.container.servlet.ServletContainer$InternalWebComponent.initiate(ServletContainer.java:278)
at com.sun.jersey.spi.container.servlet.WebComponent.load(WebComponent.java:566)
at com.sun.jersey.spi.container.servlet.WebComponent.init(WebComponent.java:211)
at com.sun.jersey.spi.container.servlet.ServletContainer.init(ServletContainer.java:333)
at com.sun.jersey.spi.container.servlet.ServletContainer.init(ServletContainer.java:497)
at javax.servlet.GenericServlet.init(GenericServlet.java:212)
at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1172)
at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:992)
at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:4058)
at org.apache.catalina.core.StandardContext.start(StandardContext.java:4371)
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:791)
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:771)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:525)
at org.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:830)
at org.apache.catalina.startup.HostConfig.deployWARs(HostConfig.java:719)
at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:490)
at org.apache.catalina.startup.HostConfig.check(HostConfig.java:1217)
at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:293)
at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:117)
at org.apache.catalina.core.ContainerBase.backgroundProcess(ContainerBase.java:1337)
at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1601)
at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1610)
at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.run(ContainerBase.java:1590)
at java.lang.Thread.run(Thread.java:595)
[Reply]