Java Reference
In-Depth Information
HTTP
HTTP
/
1.1 200
OOK
Content-Type
:
application/xml
Link: <http://example.com/orders/333/cancelled>; rel=cancel
<order id="333">
...
</order>
The
Link
header has all the same characteristics as an Atom link. The URI is enclosed within
<>
followed by one or more attributes delimited by semicolons. The
rel
attribute is required
and means the same thing as the corresponding Atom attribute of the same name. This part
isn't shown in the example, but you may also specify a media type via the
type
attribute.
Personally, I really like
Link
headers as an alternative to embedding Atom links. Many
times, I find that my client isn't interested in the resource representation and is only interes-
ted in the link relations. You shouldn't have to parse a whole XML or JSON document just to
find the URL you're interested in invoking on. Another nice thing is that instead of doing a
GET invocation, you can do a HEAD invocation and avoid getting the XML document en-
tirely. In general, I like to use Atom links for data aggregation and
Link
headers for
everything else.
HATEOAS and JAX-RS
JAX-RS doesn't have many facilities to help with HATEOAS. HATEOAS is defined by the
application, so there's not much a framework can add. What it does have, though, are helper
classes that you can use to build the URIs that you link to in your data formats.
Building URIs with UriBuilder
One such helper class is
javax.ws.rs.core.UriBuilder
. The
UriBuilder
class allows
you to construct a URI piece by piece and is also sensitive to template parameters:
public
public abstract
abstract class
class
UriBuilder
UriBuilder
{
public
public static
static
UriBuilder
fromUri
(
URI uri
)
throws
throws
IllegalArgumentException
public
public static
static
UriBuilder
fromUri
(
String uri
)
throws
throws
IllegalArgumentException
public
public static
static
UriBuilder
fromPath
(
String path
)
throws
throws
IllegalArgumentException
public
public static
static
UriBuilder
fromResource
(
Class
<?>
resource
)
throws
throws
IllegalArgumentException
public
public static
static
UriBuilder
fromLink
(
Link link
)
throws
throws
IllegalArgumentException