2014-02-02

Setting up a Jersey app on Tomcat 7.0.50 with Eclipse 4.3 and Maven 3.0.4

It took me the past 2 days to learn what Maven is and how to use it in Eclipse; what Jersey is, and how convenient to use it to implement a JAX-RS app on Tomcat; and what's new in Servlet 3.0, and how to deploy an app without a web.xml.

Maven is the apt-get for jars. Descriptor-less deployment seems to make plug-n-play more easily. JAX-RS annotations make REST on Servlet platform simpler.

Alright, after several experiment failures, I figured out a way to get Jersey working on Tomcat with help from Maven and Eclipse.

So real stuff:
  1. Create a "Dynamic Web Project".
  2. Right click on the new project, "Configure" > "Convert to Maven project...".
  3. Add Jersey dependency to pom.xml: (this MUST be added with a "compile" scope, "provided" will fail to let Tomcat discover the REST app; for other containers/platforms, read this)
    groupId: org.glassfish.jersey.containers
    artifactId: jersey-container-servlet
    version: 2.5.1
  4. Save pom, Maven update.
  5. Create a class extends javax.ws.rs.core.Application, annotated with javax.ws.rs.ApplicationPath.
  6. Create a class, annotated with javax.ws.rs.Path. Individual methods of this class should be properly annotated with javax.ws.rs.GET etc. for handling requests.

No comments: