users@jersey.java.net

[Jersey] Serving up JSON, XML and HTML (via templates) elegantly

From: Damian Steer <d.steer_at_bristol.ac.uk>
Date: Mon, 23 Jul 2012 16:51:41 +0100

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hi all,

I have a fairly simple requirement: send out model objects in xml,
json and html. Jersey is pretty good at this, but the following makes
me just a little bit sad:

    @GET
    @Path("{id}")
    public Person get(@PathParam("id") Integer id) {
        return personMapper.get(id);
    }

    @GET
    @Path("{id}")
    @Produces("text/html")
    public Viewable getHTML(@PathParam("id") Integer id) {
        return new Viewable("/person", get(id));
    }

Two methods where one ought to be enough?

It might be possible to just return a Viewable, but the JSON / XML
magic breaks (that ought to be fixable via Viewable#getModel).

Implicit viewables, OTOH, are resistant to my googling. I couldn't
find any mention of extracting path parameters for them.

I did find an Atlassian page [1] which mentioned something along the
implicit viewables line, but for model objects (e.g.
com/example/Person/index.jsp). Sounded like an Atlassian specific
feature, alas.

Currently the obvious solution is rolling my own provider for HTML
which will tidy things up, but I'm surprised it doesn't exist already.

Does anyone have any other suggestions?

Thanks in advance,

Damian Steer

[1]
<https://ecosystem.atlassian.net/wiki/plugins/viewsource/viewpagesrc.action?pageId=6094924>
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.11 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAlANcwUACgkQAyLCB+mTtymdagCfRGtEVi+Vv103OMUNh3GUQbWw
vTUAoPblK4VBGXHsm358YD+oKtUUe1BO
=T3s3
-----END PGP SIGNATURE-----