Skip to content
josedonizetti edited this page Feb 9, 2011 · 1 revision

Hypermedia clients

Hypermedia support information is scattered aroud the entire documentation as this is a hypermedia based library..

The response object

The response object to a restfulie http request allows access to four different methods, the response code, its body, headers and an extra method called resource that will try to unmarshall the response body according to its content type.

Restfulie uses medie to do the unmarshalling, giving back a hypermedia aware object. The following example shows a typical response to a xml request:

Response response = Restfulie.at(uri).accept("application/xml").get();
System.out.println(response.getBody());
Rest from scratch book 1235.4
System.out.println(response.getCode()); // 200
System.out.println(response.headers('Content-Type')); //'application/xml'
Order oder = response.getResourse();
System.out.println(order.getItems(0).getId()); // "Rest from scratch book"

Supported media types

Restfulie client comes with built in support to xml, json and opensearch and can be easily enhanced by providing your own driver through Medie. Check out medie's documentation on how to support your own media type or contribute with Restfulie by posting your media type handler at the mailing list.

Link support

For representations that contain link elements in its body, such as this one in xml:

Rest from scratch book 1235.4

This link can be accessed by unmarshalling the resource and then accessing the link itself:

Response response = Restfulie.at(uri).get();
//follow the payment link within the order
Order order = response.getResource();
// the resource method is staticaly imported from ...
resource(order).getLink("payment").follow().get();

//will print the payment body
System.out.println(order.response.getBody());

Once you have invoked the follow method, the object returned is the same as a Restfulie.at() invocation, a request dsl, which allows you to configure the request as you wish, and execute it by invoking the proper http verb (i.e. get, post or put).

You can see an example of a full client navigating through links in the examples section.

Link header support

If your service provides a link header such as:

Link: http://amundsen.com/examples/mazes/2d/ten-by-ten/0:north; rel="start"; type="application/xml"

This header can be accessed by using the headers.getLink("rel") and headers.getLinks() methods:

Response response = Restfulie.at(uri).get();
System.out.println(response.getHeaders().link("start").getHref());

# following the link
Resource resource = response.getResource();
Response anotherResponse = resource.getHeaders.getLink("start").follow().get();
Clone this wiki locally