users@jersey.java.net

Putting the RESTful "connectedness" around my existing Domain objects

From: Brett Dargan <brett.dargan_at_gmail.com>
Date: Thu, 4 Dec 2008 14:54:58 +1000

One of the great things about jersey is that it gives us a simple way to
take our existing JAXB annotated objects and expose them as resources.

So at my company we are progressing with creating more RESTful Web Services,
I would like a standard way to facilitate adding the "connectedness" aspect
of existing JAXB annotated objects.

Obviously right now, we don't have domain objects that are linked in that
way.

Has anyone come across this problem?


Maybe we could create some Annotation, add to our existing JAXB annotated
objects that use the same URITemplate style specified in the jsr311.

Then by using a ContainerResponseFilter, traverse the entity object and add
links to those objects prior to unmarshalling.

I've got some running code that does this, nothing worth posting yet.

But there are a few issues, I'm not sure if I should continue down this
path, or if someone has a better idea.

So something like this:

@XmlRootElement

class Book {

private String id;

@ResourceLink("/authors/{author}")

private String author;

@ResourceLinkContainer

private List<ResourceLinkItem> resourceLinks = new
ArrayList<ResourceLinkItem>();
...

Then in the ContainerResponseFilter
grab the entity out of the contentResponse, scan it for ResourceLinks, add
ResourceLinkItems with hrefs that have been evaluated, from some context.

Since we support multiple representations, the ResourceLinkItem, would need
to handle links like standard html as well as xlink.

The other potential niceness, is being able to preserve the suffix in links
from the requested resource.

Eg. Requested specific representations, such as /books/1231.xml should be
able to return an xml representation with the same suffix, like
/authors/bob.xml.

Or /books/1231.es.html should contain a link to /authors/bob.es.html

Using a filter, would enable me to easily slightly tailor the links to other
resources, without affecting the original domain object.

Sure it raises questions like what is a suffix, but there are conventions we
could follow around that.

cheers,

Brett.