Skip to content
josedonizetti edited this page Feb 15, 2011 · 4 revisions

Java client

Entry points

Entry points are commonly defined through a resource retrieval or creation request. Restfulie allows you to use both type of entry points through its API.

Resource retrieval entry point

Most systems will create a request retrieval entry point, which can be accessed as:

Response response = Restfulie.at('http://localhost:3000/cities/5').get(); City city = response.getResource();

After that, the xml tree can be accessed and links followed. A typical city hypermedia file would be:

<city>
	<name>Sao Paulo</name>
	<population>
		<size>18000000</size>
		<growth>10</growth>
	</population>
	<link rel="next_largest" href="http://localhost:3000/cities/18" />
</city>

The information can be retrieved through the usual method invocations:

System.out.println(city.getName());
System.out.println(city.getPopulation().getSize());
System.out.println(city.getPopulation().getGrowth());

And links can be followed as:

Link link = resource(city).getLink("next_largest");

Note that the client application knows what the rel attribute next_largest means, but does not know what it's value stands for (Rio de Janeiro).

Acessing the web response

In this case, you can access the http response through response, i.e.:

Response response = Restfulie.at('http://localhost:3000/cities/5').get()
System.out.println.("Response code" + response.getCode());

Parsing a web response

The return of such web_response method is an object of Restfulie::Client::HTTPResponse and allows you to access methods to check whether the request was successful or any other group of response codes:

	Response response = Restfulie.at('http://localhost:3000/cities/5').get();
	puts "Response code #{city.web_response.is_successful?}"
	puts "Response code #{city.web_response.is_client_error?}" do we have it in java?

Resource creation entry point

If your server defines an entry point related to a resource creation, you can use the create method as:

Response response = Restfulie.at('http://localhost:3000/cities').post(city);
City city = response.getResource();

Note that city seems to be the result of following a 201 response to its given Location header.