Hi,
On 06/02/12 22:55, hdave wrote:
I have a fairly rich domain model that is marshalled to XML and JSON as part
of a REST API. In keeping with HATEOAS I would like to return link objects
for many of these entities to provide URL/link relations for clients to use.
To do this, I could add a new property to (all of) my domain classes named
"links" of type "List<RestLink>". This has the benefit that it would get
picked up by the existing marshallers for XML and JSON. But I'd prefer to
not pollute my domain model with web service concerns if possible.
That said, I *really* do not want to create 100+ JAX-B XML adapters either.
Any ideas on a good way to approach this? Is there such a thing as a
universal JAX-B adapter? Are their marshalling events that I could hook
into to do this? Just trying to think of an elegant AOP-like approach.
For XML, one option is to get a CXF JAXBElementProvider subclass
registered which will override its protected getStreamWriter method and
return a custom XMLStreamWriter (see [1] for an example), this writer
will write some attributes or elements representing the links.
Do you expect such links added as attributes or a root element's
children ? There's some effort under way in JAX-RS 2.0 to get the
auto-generation of links supported, but as far as I recall the original
proposal was complex.
I'm wondering if we should introduce Link (which would offer a name -
say 'link' or 'href' etc, position - as attribute or element, relation
if any) and a composite Links annotations...The tricky bit with these
annotations is how to express that say the "next" resource can be
identified at http://someaddress/3 and the previous at
http://someaddress/1...
The custom writer approach, while being a bit involved, is definitely
most flexible...
What sort of links are you thinking about ?
Sergey
[1]
http://svn.apache.org/repos/asf/cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/CustomXmlStreamWriter.java
--
View this message in context:
http://cxf.547215.n5.nabble.com/Good-way-to-add-link-relations-to-REST-responses-tp5461580p5461580.html
Sent from the cxf-user mailing list archive at Nabble.com.
--
Sergey Beryozkin
Talend Community Coders
http://coders.talend.com/
Blog: http://sberyozkin.blogspot.com