Rest easy
I’ve been playing around with REST (REpresentational State Transfer) for a bit and decided to write a small tutorial to show how easy it is to create a rest interface to your business objects
Advantages of rest
- Works over HTTP, known protocol with known data formats (mime types), no weird ports open in your firewall
- Every resource is uniquely identified by a URI
- Can do CRUD on those resources (they are just called POST, GET, PUT, DELETE)
- used by all the major players for cloud applications (SUN's cloudApi, Microsoft's Azure services platform)
- we might finally get rid of webdav
I'll be using the Jersey Framework for this.
As i’ve been working on a backend that will allow you to manage agile development processes (Scrum, Kanban etc) I thought I use that.
In this application every object is represented by a Card for instance a Story, Task, Bug, work Item, etc.
Cards have a hierarchical structure and can each have custom properties
The class looks something like this (removed methods for clarity):
public abstract class Card { private long id; private List<Card> children = new ArrayList<Card>(); private List<Entry> properties = new ArrayList<Entry>() }
and then for instance the Story class with some default values set in the constructor:
public class Story extends Card { public Story() { addProperty("Title", "A new story"); addProperty("Description", "as a .. I want .. for the benefit of .."); addProperty("Storypoints", "0"); } }
To be able to return these objects through REST they need to be in a format that can be distributed over HTTP, in this case we will be using XML and JSON.
The Java XML Bindings are perfectly suited to create xml representations of our classes
@XmlRootElement(name="card") public abstract class Card { private long id; @XmlAttribute(name="type") protected String instance = this.getClass().getSimpleName(); @XmlElement(name="card") private List<Card> children = new ArrayList<Card>(); @XmlElement(name="property") private List<Entry> properties = new ArrayList<Entry>(); @XmlAttribute public long getId() {} }
* the Entry class is a simple class with a name and value property, I choose this approach as JAXB has an issue in representing HashMaps() in XML
this will produce the following xml
<card id="4" type="Task"> <property value="nobody" name="assigned"/> <property value="Slay dragon" name="Title"/> <property value="his weak spot seems to be a small spot on his belly" name="Description"/> <property value="4" name="hours"/> </card>
for JSON we don’t need to do anything special then include the jersey-json.jar to the project.
Now to configure and hookup the rest servlet
I added the jersey servlet to my web.xml
<servlet> <servlet-name>Rest Web Service</servlet-name> <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>Rest Web Service</servlet-name> <url-pattern>/services/*</url-pattern> </servlet-mapping>
And create the service provider class
@PerRequest @Path("/agileassistant") @Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML }) @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML }) public class AgileAssistantService { ... }
This will tell Jersey that this is a provider that will respond to requests to /rest/agileassistant, accept JSON and XML and can output JSON or XML depending on the accept headers sent by the client
Now if we for instance want to return the first card (rootCard) and all its children we add this method
public static Card rootCard = Card.createCard(Root.class); @GET public Card getRoot() { return rootCard; }
(in my data model the rootCard is always present and functions as a starting point to add the rest of the cards in a hierarchical structure)
So requesting this card will effectively return the entire structure for my test data:
<card id="1" type="Root"> <card id="2" type="Story"> <property value="Convince King" name="Title"/> <property value="to let the prince go on an adventure" name="Description"/> <property value="5" name="Storypoints"/></card> <card id="3" type="Story"> <card id="4" type="Task"> <property value="nobody" name="assigned"/> <property value="Slay dragon" name="Title"/> <property value="his weak spot seems to be a small spot on his belly" name="Description"/> <property value="4" name="hours"/> </card> <card id="5" type="Task"> <property value="nobody" name="assigned"/> <property value="Rescue Princess" name="Title"/> <property value="She'll be the one screaming from the tower window" name="Description"/> <property value="2" name="hours"/> </card> <property value="Once upon a time" name="Title"/> <property value="in a land far far away" name="Description"/> <property value="5" name="Storypoints"/> </card> </card>
Doing a parameterized query is almost as easy
@GET @Path("/card/{id}") public Card getCard(@PathParam("id") long id) { return getCardById(rootCard, id); }
here the id that you specify will be injected into the parameter
giving the following output
<card id="4" type="Task"> <property value="nobody" name="assigned"/> <property value="Slay dragon" name="Title"/> <property value="his weak spot seems to be a small spot on his belly" name="Description"/> <property value="4" name="hours"/> </card>
To conclude enabling rest for your applications is pretty easy, writing this blog took longer than writing the code.
In a next post I will add POST/PUT/DELETE requests to store data in your application