users@jersey.java.net

Generating urls and controlling rendering/serialization

From: Jo Størset <jo.storset_at_usit.uio.no>
Date: Thu, 28 Feb 2008 13:23:17 +0100

Hi,

let me say I'm plesantly surprised by the quality and ease-of-use of
jsr-311 and jersey, even at this early stage. However, there are a
couple of things that bothers me, and I thought it might be a good
idea to ask on this list (sorry for the long post :)

1) Controlling rendering/serialization

I like the idea of just returning domain "model" objects from resource
objects, and let a seperate "view layer" in the framework resolve and
generate the view. This works pretty well with jaxb and xml out-of-the-
box, so with just a little bit more ability to control this i would be
well underway to have everything I need for my app.

I've tried to make a freemarker template processor modeled after the
jsp variant, but it doesn't seem to get called as expected. Is this
not supposed to work yet, or have I missed how I register this
provider correctly. The documentation is a bit sparse, and the source
code is a bit to complex for me to immediately figure out how things
work :)

Another thing I'd like is to be able to add a combination of xml
serialization (jaxb) combined with an xsl transformation to be able to
produce a html view. Would this be possible?

As a third option, I'd like to add a simple processing instruction
with an xsl reference to the resulting jaxb xml. I haven't found an
elegant way to do this with jaxb, and I suspect I need to tweek the
jaxb messagewriter or replace it with a custom handler. Any idea how i
could do this elegantly?

2) Generating urls

I have seen some this discussion of this before, but haven't really
found any good recommendations to this important topic. So I give it
another go :)

What makes rest work is the interlinking of resource representations,
so it's fairly important for links to other representations to be
present in representations. Given that I want to mainly avoid custom
generation of the representations in the resources themselves (se 1),
this seems like the biggest problem I have with the jsr-311 approach.

For now I have done a hack by adding a url bean property to my domain
objects (the ones that represent rest resources), and on my @GET
methods I explicitly write this property before returning the object
(for jaxb serializing). Is there any way to avoid this?

In addition to requiring my domain objects to have this property
(which really isn't nice), there's a couple of additional things that
bothers me.

The first problem I have is the fact that every resource needs to
explicitly know how to generate urls to every other resource(type)
that is to be linked in the representation. This leads to *a lot* of
duplication, and I don't like a so central part of a rest based
application to lead to such an amount of manual and error prone
duplication.

Maybe I haven't understood the model good enough, but the second
problem I have is that the jsr-311 model seems to dictate a
duplication of the domain model in a seperate resource layer on top.
All my resource objects need to know the mapping between the domain
objects and the correponding resource objects containing the
annotation mapping (and usually som hard coded path attibutes as well)
for every link it is to generate.

In reality I end up with hard coded duplication of the annotation
based request mapping everywhere I want to represent hyper links.
Since almost all the necessary information to be able to create links
has been added to the application through the jsr-311 annotations,
this really doesn't feel right. Can anyone enlighten me, I'm kind
hoping that I have misunderstood something fundamental here :)

Do you have any suggestions for how to do this in a better way?

Jo