Creating a simple Java RESTful service using Jersey and Maven
There has been a lot of interest in Resource Orientated Architecture (ROA) online which has resulted in an explosion of excellent libraries that make it simple to use this architectural style.
The example in this article uses Java, Maven and a JAX-RS library called Jersey to create a very simple orders system.
Our order consists of a reference number and a customer name. The system has three entry points:
- HTTP PUT to create an order
- HTTP GET to see the details of an order
- HTTP GET to see all the existing orders
Download the full source of this article here, or fork it on Github
To make it simple to get it up and running I've added configuration for Jetty to run on port 9090. You can compile the source code and run a web server to use it by issuing this single Maven command:
I recommend that you use curl to test your RESTful web services as it is easy to change the HTTP verb that you are using. You can manipulate the orders system using these curl commands.
Note: HTTP GET is the default verb used by curl, I've explicitly listed GET in the examples to make it clear which verb is in use.
The code
To use Jersey you need to add a servlet to your web.xml and create a resource implementation class.
web.xml
The WEB-INF/web.xml fragment sets up the Jersey Servlet with a parameter listing the package to search for RESTful classes.
This reads any classes in the com.joejag.code.orders.restservices and looks for annotations declaring resources.
The Java class
On the implementation class you can see the @Path annotation on the class signature indicating the resource we are handling. This is used by Jersey to configure the URL used to interact with the resource.
Each method has a sub-path declared, an HTTP verb to respond from and the response type to produce. The method parameters are bound by using either part of the path (with @PathParam) or a separate query parameter (with @QueryParam).
Download the full source of this article here, or fork it on Github
Where to learn more
There are a number of great articles on how to develop JAX-RS REST applications. I recommend you start with the Jersey guide for Java applications:
If you are using Ruby then take a look at the wonderful Sinatra project.