Jersey is the open source JAX-RS implementation for building RESTful Web services. JAX-RS uses java annotations to simplify the development and deployment of web service clients and endpoints. In this post I'll describe how I've implemented a naive RESTful web service for storing and querying a DNA database. This code was tested and deployed under netbeans 6.1.
The service is defined in a class named
FastaRest. An annotation @Path gives the root of our services. The value of UriInfo is injected by @Context (this interface provides an access to the application and requests information about the current URI). Our DNA database is just an associative array mapping the DNA sequence to its id (integer).
Building the Resource
Adding a DNA sequence
A DNA is inserted using a PUT method. The relative path must be
/addand two parameters (@FormParam= id, an integer and @FormParam= seq, the DNA sequence) must be provided. A JSON response is returned with a message telling if the new sequence was added.
Retrieving a DNA sequence
The Fasta sequence of a given DNA is returned to the client using a GET method with the relative path
Dump all the DNA sequencesThe fasta sequences of all the DNA is returned to the client using a GET method with the relative path
/seqs. As this result may be huge, a StreamingOutput object is returned rather than a String.
Add a sequence
Add a second sequence
Try to re-insert sequence id=2
Try to insert something that is not a DNA sequence
Retrieve sequence id=1
That's it !