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 …
You may also like:

Follow on Twitter
#1 by Rix Beck on February 22nd, 2009
Quote
Hi,
thanks for you post. I’m sitting the same boat using Eclipse intensively and I love it so I find all the solutions that makes developing more comfortable within.
Tried NetBeans’ restful services and faced that is dirty comfortable. (Check here: http://www.netbeans.org/kb/docs/websvc/rest.html)
I would be glad if it is reproducible under Eclipse.
cheers Rix
[Reply]
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]
#2 by Manjit on June 12th, 2009
Quote
Thanks for the post. I use Eclipse/Ant, and your post was the only one that helped me with clues on how to make this work without Netbeans/Maven/Glassfish combo.
[Reply]
Surya Suravarapu Reply:
June 13th, 2009 at 11:46 am
I’m glad you liked it
[Reply]
#3 by Thierno on July 7th, 2009
Quote
very good
[Reply]
#4 by pramir on July 24th, 2009
Quote
Hi
I tried this tutorial step by step, but couldn’t get it to work.
I didn’t do any configuration outside of these steps (Please tell me if I have to do any).
Tomcat gives me error 404 when i try the URL in step 5.
My directory structure (in webapps) is:
restdemo
WEB-INF
web.xml
lib > All jersey jasr you mentioned
classes > com.suryasuravarapu.jersey > HelloWorldResource.class
[Reply]
Mike Reply:
July 24th, 2009 at 10:35 pm
I believe that you can specify an init-param in the servlet config definition that tells Jersey what java packages to scan to look for annotations. Try:
*Servlet Name*
com.sun.jersey.spi.container.servlet.ServletContainer
com.sun.jersey.config.property.packages
*Java Packages seperated by semi-colons*
1
[Reply]
Mike Reply:
July 24th, 2009 at 10:36 pm
Check out this website to see what I mean: http://kingsfleet.blogspot.com/2008/10/running-jax-rcjerseyjsr311-on-weblogic.html
[Reply]
#5 by pramir on July 25th, 2009
Quote
Hi Mike,
Thanks for your reply, I appreciate it.
But I still have no luck. I tried putting in the two init params. My web.xml looks like this:
jersey
com.sun.jersey.spi.container.servlet.ServletContainer
com.sun.jersey.config.property.resourceConfigClass com.sun.jersey.api.core.PackagesResourceConfig
com.sun.jersey.config.property.packages com.suryasuravarapu.jersey
1
jersey
/*
And this is the URL I am trying: http://localhost:8080/restdemo/helloworld
On this resource:
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”;
}
}
I can’t understand why it doesn’t work. Am I missing some jar configuration? The only jersey jars that I have are in my lib directory. There is absolutely NO configuration that I did except having those jars in my webapp > proj ctx > web-inf > lib.
Help.
[Reply]
Surya Suravarapu Reply:
July 28th, 2009 at 7:29 pm
sorry for the delay in responding. Just want to check back and see if you got the issue resolved.
[Reply]
#6 by wave on August 3rd, 2009
Quote
Didn’t work, says it is missing a resource config or something.
[Reply]
Michael Hausenblas Reply:
August 4th, 2009 at 3:24 am
Surya,
Thanks for the nice intro – worked fine for me. I’m using Eclipse Galileo on Mac with Jersey 1.1.1.
One thing you could mention (and I think this maybe confused some, here) is that if you create a project (you chose ‘RestExample’), then the actual URI to use is http://localhost:8080/RestExample/helloworld …
KUTGW!
Cheers,
Michael
[Reply]
Surya Suravarapu Reply:
August 4th, 2009 at 8:50 pm
Micahael,
thanks! for your comments. I’ve modified the URL in the last step for clarity-sake.
[Reply]
#7 by William Antônio Siqueira on October 19th, 2009
Quote
Thanks, this was useful for me!
[Reply]
#8 by Pulak on November 3rd, 2009
Quote
Thanks Surya, this works for me and this is really a good help!
[Reply]
#9 by Niv on November 15th, 2009
Quote
Hello,
Thanks for the excellent tutorial. In my case, Apache did not recognize the ServletContainer until I copied the jars into the lib directory of Tomcat. This is not so obvious to beginners like me.
Niv
[Reply]
#10 by xmariachi on November 17th, 2009
Quote
Same here Niv, only way I could get it to work.
Possibly on the blog post example these libs where added at some point on eclipse to the tomcat external libraries on “Run As Server” configuration?
Thanks for the post anyway.
[Reply]
#11 by xmariachi on November 18th, 2009
Quote
I have a problem here.
When using this configuration, I have a problem serving the index.html page – which doesn’t get served.
I can serve the html by adjusting the servlet mapping to a more specific part of the URL, so the index.html is not taken by the ServletAdaptor. Example:
JerseyTest
com.sun.jersey.server.impl.container.servlet.ServletAdaptor
1
JerseyTest
/something-in-the-beginning-of-url/*
But doing this, the match is not done with the Resource!! So the Resources are not served.
I’m getting kinda crazy with this, since it’s absurd I cannot make it to serve both html and Resources.
Are you able to serve an index.html from within the context?
Please help!
[Reply]
#12 by xmariachi on November 18th, 2009
Quote
Hi, a solution to my problem above in my blog post
http://xmariachi.blogspot.com/2009/11/jersey-tomcat-rest-servlet-mapping-post.html
[Reply]
#13 by niv on November 27th, 2009
Quote
Hey xmariachi,
I fixed it by putting the jars in the WebContent/web-inf/lib. I guess this is really elementary, but I’m still learning. Hope it works for you too.
Niv
[Reply]
#14 by Mak on February 4th, 2010
Quote
you’re awesome! thanks for the writeup … now I’m on to the deeper waters – CRUD with JAX-RS
[Reply]
#15 by Mohamed Moubarak on February 6th, 2010
Quote
Ure the man! i have two books on writing RESTful web services using java and jax-rs..and all i wanted to know was how the hell do i start with an IDE like eclipse.
Thanks
[Reply]
#16 by Rahul on February 27th, 2010
Quote
Hi Sir,
i followed these steps but got the error…
javax.servlet.ServletException: Servlet.init() for servlet JerseyTest threw exception
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
root cause
java.lang.NullPointerException
com.sun.jersey.spi.scanning.WebAppResourcesScanner.scan(WebAppResourcesScanner.java:81)
com.sun.jersey.spi.scanning.WebAppResourcesScanner.scan(WebAppResourcesScanner.java:75)
com.sun.jersey.api.core.ScanningResourceConfig.init(ScanningResourceConfig.java:69)
com.sun.jersey.api.core.WebAppResourceConfig.init(WebAppResourceConfig.java:96)
Please help me out in this…
[Reply]
#17 by Rahul on February 27th, 2010
Quote
Hi,
As per some links regarding the error, i tried giving the following in the web.xml,
JAX-RS Servlet
jersey
com.sun.jersey.spi.container.servlet.ServletContainer
com.sun.jersey.config.property.resourceConfigClass
com.sun.jersey.api.core.PackagesResourceConfig
com.sun.jersey.config.property.packages
jersey
/*
But got the error..
javax.servlet.ServletException: Servlet.init() for servlet jersey threw exception
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:852)
org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
java.lang.Thread.run(Unknown Source)
root cause
com.sun.jersey.api.container.ContainerException: The ResourceConfig instance does not contain any root resource classes.
com.sun.jersey.server.impl.application.WebApplicationImpl.processRootResources(WebApplicationImpl.java:1064)
Can you please help me out?
Rahul
[Reply]
#18 by Rahul on February 27th, 2010
Quote
Got it…silly mistake…
My Eclipse was putting the classes in a different directory rather than WEB-INF/classes.
[Reply]
#19 by Anish Shah on March 2nd, 2010
Quote
Hey Surya,
Do you know how to deploy Jersey with Jetty
I have been using the below link to do my Jetty stuff..
http://wikis.sun.com/pages/viewpage.action?pageId=21725365
but my client is not able to find the URL and gives me 404 error..
Hope you could put some light on it..
Thanks,
– Anish
[Reply]