views:

13810

answers:

3

Hi,

I was wondering if anyone could post or know of instructions on creating a simple restful web service with eclipse and deployed on tomcat.

+12  A: 

I'd use JAX-RS, a very simple, minimalist, Java API for developing RESTful web services.

It's reference implementation, Jersey, is very good and has a very active community. The developers answer practically every question. There are also a couple of other implementations, such as RESTEasy.

Reference material

Reading the Overview of JAX-RS 1.0 Features should introduce you to most of the functionality of the API. In the Getting Started guide is information on how to include Jersey as a dependency into your project if you're using Maven or Ant (I strongly recommend using Maven). The wiki contains a lot more info and examples, but you shouldn't need them to start working.

Packaging

In many examples and in the Getting Started guide I linked to, you'll see them creating a Main class which starts the Grizzly Web container and sets the root resource. I would avoid doing that and instead extend the JAX-RS's Application class like described here and build a WAR. If you're using Maven just set <packaging>war</packaging> in the POM and run mvn package and that's it!.

That way you can drop it in any Servlet container, including Tomcat.

Testing while developing

I don't know if Tomcat has a similar Maven plugin, but you can run your project "exploded" (without requiring repackaging) using Jetty with this one-liner:

mvn org.mortbay.jetty:maven-jetty-plugin:run -Djetty.scanIntervalSeconds=3

The scanIntervalSeconds option configures the plugin to refresh your classes every three seconds. This way it picks up changes as soon as you save your files, so you can develop pretty much the way you would when using an interpreted language such as PHP.

Enjoy and see you on the mailing lists :)

Jaka Jančar
+2  A: 

I think this is what you want, it's a nice and simple starting point:

RESTful Webservices with Java and Jersey (JAX-RS) - Tutorial by Lars Vogel

Quicksilver