- Download and unjar the Jersey distribution (I used 0.8) from https://jersey.dev.java.net/ to a location on your system (lets call it
JERSEY_HOME
for this article). - Download and install the Java-WS distribution (I used 2.1) from https://jax-ws.dev.java.net/. (lets call it
JAXWS_HOME
for this article). - Rama Pulavarthi wrote a blog (in fact the key for me) in configuring tomcat to reference the Jersey distribution jars. To summarize, in
TOMCAT_HOME/conf/catalina.properties
modify theshared.loader
property entry to point to yourJAXWS_HOME/lib/*.jar
. Mine looks like this:shared.loader=C:/dev/jaxws-ri/lib/*.jar - Create a new Dynamic Web Project in Eclipse using Tomcat 6.0 as the application server.
- Create a new library in which you add the following JARs:
- JERSEY_HOME/lib/asm-3.1.jar
- JERSEY_HOME/lib/jersey.jar
- JERSEY_HOME/lib/jsr311-api.jar
- Next modify the
web.xml
to include the adaptor for jersey. Most of the Blogs refer to a different class than what appears in 0.8 I am not certain which is the right class, only the following works for me (and the documented one is not available throught the downloads!):<servlet> <servlet-name>ServletAdaptor</servlet-name> <servlet-class>com.sun.jersey.impl.container.servlet.ServletAdaptor</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>ServletAdaptor</servlet-name> <url-pattern>/resources/*</url-pattern> </servlet-mapping> <session-config> <session-timeout>30</session-timeout> </session-config> - If you start Tomcat, you will see the following error:
This error is due to not providing any root RESTful resources.com.sun.jersey.api.container.ContainerException: The ResourceConfig instance does not contain any root resource classes. - Create a new class and include the following:
import javax.ws.rs.GET; import javax.ws.rs.ProduceMime; import javax.ws.rs.Path; @Path("/test") public class TestResource { @GET @ProduceMime("text/html") public String getMessage( ) { return "hello"; } }
- Start and test the application with the following url:
and you should see a hello in your web-browser.http://localhost:8080/appName/resources/test
Thursday, October 2, 2008
Deploying Jersey in Tomcat 6.0
Jersey is a reference implementation for the JAX-RS (JSR-311) for building RESTful Web services. It is nearing approval with the JSR committees. While there are many wikis and articles in using Jersey with Netbeans (downloading the Netbeans 6.1 EE package includes everything you need for JSR-311), there was very little information on using Tomcat 6.0 with Jersey. After piecing together several blogs, I was able to get a simple resource working in Tomcat using JSR-311.
The following are the steps I used to get this working:
Subscribe to:
Post Comments (Atom)
5 comments:
For jersey 1.1.0-ea :
- add jersey-bundle-1.1.0-ea.jar
- in the web.xml change the servlet-class by com.sun.jersey.server.impl.container.servlet.ServletAdaptor
- also change the @ProduceMime annotation by @Produces and the import by import javax.ws.rs.Produces
Yes, you are correct. These changed with Jersey 1.0, I just did not go back to update the Blog. I will take this as an action to modify the entry or post a new one.
I just came across your blog while trying to figure out how to deploy a rest service I'm working on. This completed the puzzle for me. Thank you!
I just came across your blog while trying to figure out how to deploy a rest service I'm working on. This completed the puzzle for me. Thank you!
Thanks Jason for the quick start tutorial.
Post a Comment