users@jersey.java.net

[Jersey] Controlling multiple projections from one data-model?

From: Casper Bang <casper.bang_at_gmail.com>
Date: Thu, 3 Mar 2011 11:31:53 +0100

It is extremely handy to be able to leverage JAXB binding such that Jersey
can automatically serialize to both JSON and XML from one data-model.
However, when working with multiple fine-grained resources it quickly
becomes hard to separate internal data-model from a resource projection
(controlling depth and breath).

I see 3 ways to move forward:

1) Specifically define two resources with each their Accept mime-type, and
work with raw Jackson and XML nodes. I've done this in the past but it feels
crude and primitive.

2) Somehow manually assembling JAXB nodes such that I may traverse/visit one
data-model, but only emit/project a subset from a resource? I'd need control
over the JAXB model and be able to return this from a resource, allowing me
to still reek the benefits of the nifty JSON/XML serializers. With
this approach, I'd also expect to be able to plug in custom serializers.

3) Try to use the type-system to define the projections statically i.e. let
the data-model be passed along to the copy-constructor of the root of
whichever projection hierarchy that's needed. This would be my preference,
even if I am not really a fan of shadow hierarchies and class bloat.

I'd like to know what people generally do since I am hardly in a unique
situation here. Seems to be rooted in the same static-dynamic impedance we
see in so many other places and where expression trees (C#'s anonymous
types, object initializers and type inference) would come in veeery handy.

/Casper