Skip to main content

Declarative HTTP Link Headers

Posted by mhadley on March 18, 2010 at 2:15 PM PDT

I've extended the declarative hyperlinking module to support the HTTP Link header. It works similarly to the @Link annotation I described earlier except you annotate the response entity class with @LinkHeader (or @LinkHeaders if you need more than one Link header) instead of annotating response entity fields with @Link. Here's a complete example that shows both annotations in action:

@LinkHeader(value=@Link(resource=WidgetResource.class), rel="self")
@XmlAccessorType(XmlAccessType.NONE)
@XmlElement(name="widget")
public class WidgetRepresentation {

  @Link(resource=WidgetResource.class)
  @XmlAttribute
  private String href;
   
  String id;
   
  public WidgetRepresentation(String id) {
    this.id = id;
  }
 
  public String getId() {
    return id;
  }
}

Given a resource class:

@Path("widgets/{id}")
public class WidgetResource {

  @GET
  @Produces("application/xml")
  WidgetRepresentation getWidget(@PathParam("id") String id) {
    return new WidgetRepresentation(id);
  }
}

A GET request for /application/widgets/10 will result in:

HTTP/1.1 200 OK
Server: GlassFish v3
Link: </application/widgets/10>;rel="self"
Content-Type: application/xml
Content-Length: xxx
Date: Thu, 18 Mar 2010 21:04:04 GMT

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<widget href="/application/widgets/10"/>

where the bold text is created by the new linking annotations.