users@jersey.java.net

Re: [Jersey] hateoas support in Jersey?

From: Craig McClanahan <Craig.McClanahan_at_Sun.COM>
Date: Wed, 16 Sep 2009 09:22:45 -0700

Felipe Gaścho wrote:
> If I want to produce an output like:
>
> <account>
> <id>ADK31242</id>
> <link href="http://bank.org/account/ADK31242" rel="self"/>
> <link rel="http://bank.org/rel/transfer"
> type="application/vnd.bank.org.customer+xml"
> href="http://bank.org/transfers"/>
> <balance currency="USD">534.62</balance>
> </account>
>
> Is there some special support in Jersey to produce such LINK and REL
> elements and attributes .. ? or should I import the HTML schema or to
> define these types by myself ?
>
>
>
>
>
There are two pieces to this puzzle.

Jersey does have support for calculating what the URI value to be
included in your link tags should be. Check out the UriBuilder[1] class
in the JAX-RS API. It lets you build up a URI, for example, by knowing
the resource class and path template arguments you want to use. You can
also google "jersey uribuilder examples" for some links to sample code
using this.

Everything about converting your response entity into an XML data
structure like this (whether it is a link or not) is totally dependent
on the mechanism you are using to create the response. A couple of
choices for this case:

* Manually build up the XML as a string yourself, and send it as the
response entity.
  There's nothing Jersey can really do for you here ... it doesn't pay
any attention to
  the actual content.

* Create classes for Account and Link that are annotated with JAXB
annotations.
  In particular on the Link class, you'll want to use the annotations
that make the
  "href" and "rel" properties come out as XML attributes, rather than as
nested elements
  (which is the JAXB default). The Account class might have a "links"
property of
  type List<Link> or something, which you could use to configure the
links. Then,
  all you need is to return the Account instance as your response entity.

Craig

[1]
https://jsr311.dev.java.net/nonav/releases/1.1/javax/ws/rs/core/UriBuilder.html