Java Reference
In-Depth Information
9.5.3. Adding structural links
Structural links in JAX-RS are instances of the Link class inside the entity itself. Convert-
ing them to XML or JSON then requires a special serializer, which is provided by the API.
Here'sthe Person class, expanded to hold the self , next ,and prev links as attributes:
@XmlRootElement
@EqualsAndHashCode
class Person {
Long id
String first
String last
@XmlJavaTypeAdapter(JaxbAdapter)
Link prev
@XmlJavaTypeAdapter(JaxbAdapter)
Link self
@XmlJavaTypeAdapter(JaxbAdapter)
Link next
}
The prev , self , and next links are instances of the javax.ws.rs.core.Link
class, as before. Link.JaxbAdapter is an inner class that tells JAXB how to serialize
the links.
Setting the values of the link references is done in the resource, this time using an interest-
ing Groovy mechanism:
Response findById(@PathParam("id") long id) {
Person p = dao.findById(id)
getLinks(id).each { link ->
p."${link.rel}" = link
}
}
The same getLinks private method is used as in the headers section, but this time the
links are added to the Person instance. By calling link.rel (which calls the getRel
method) and injecting the result into a string, the effect is to call p.self , p.next , or
Search WWH ::




Custom Search