users@jersey.java.net

Re: [Jersey] marshalling to html templates

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Mon, 22 Mar 2010 11:30:07 +0100

Hi John,

On Mar 18, 2010, at 6:18 PM, John Calcote wrote:

> Has anyone ever considered using JAXB and HTML templates to marshall
> Java objects to/from HTML?
>

There is nothing specifically in JAXB support that, so it would
require some addition stuff. An XML style sheet would IMHO be the best
fit, XML to HTML or XHTML, rather than code that operates on the JAXB
related classes.


> Seems like a useful thing to do, and I've got a use case for it at
> the moment. I have a ReST webapp and client that exchange XML
> messages. For testing and browsability, it would be nice to support
> text/html as an accept header mimetype from the browser.
>
> Thoughts?

That should be possible if you reference a style sheet with the XML.
Browsers should be able to process the XML and apply the style sheet.

In JAXB one can set the style sheet on the marshaller using the
property:

   https://jaxb.dev.java.net/nonav/2.1.10/docs/
vendorProperties.html#xmlheader
   http://n2.nabble.com/Marshaling-JAXB-objects-including-stylesheet-information-td2429726.html

In JAX-RS/Jersey you can supply a ContextResolver<Marshaller> for the
JAXB objects.

Two alternatives if the server supports such style sheet transformation:

- write a message body writer supporting JAXB and text/html and reuse
the JAXB XML support via the injected Providers interface.

- write a template provider such that one can specify the style sheet
as a view.

     return new Viewable("mystylesheet.xml", myJaxbInstance);

Paul.