Java Reference
In-Depth Information
A true [ 16 ] REST application understands that specific resource URLs may evolve, despite
attempts to keep them as stable as possible. The idea therefore is to make requests that dis-
cover the subsequent URLs to follow. We're so accustomed to having a fixed API that this
can be a difficult concept to adopt. Instead of knowing exactly what you're going to get
back from any given request, you know how to make the first request and interrogate the
result for whatever may come next. This is similar to the way we browse the web, which is
no coincidence.
16 The word true here is defined as “at least trying to follow the principles in Roy Fielding's thesis.”
It does place a higher burden on the client and the server, though. The server needs to add
somesortofmetadatatoexplainwhatthesubsequentresourcesareandhowtoaccessthem,
and the client needs to read those responses and interpret them correctly.
This section will illustrate the ways you can add links to the service responses. I'll start by
showing an example from a public API, then demonstrate how to add links to the HTTP
response headers or to the response bodies, and finally demonstrate how to customize the
output however you want.
9.5.1. A simple example: Rotten Tomatoes
As a simple example, consider the API provided by the movie review website Rotten To-
matoes used in chapter 8 on Groovy with databases. The Rotten Tomatoes API only sup-
ports GET requests, so it isn't a full RESTful service. [ 17 ]
17 RESTful services that only support GET can be called GETful services. If they're stateless, too, doesn't that make
them FORGETful services? Thank you, thank you. I'll be here all week. Try the veal, and don't forget to tip your
waitresses.
Usingthesite'sURL-basedAPItoqueryformoviesincludingtheword trek lookslikethis:
api.rottentomatoes.com/api/public/v1.0/movies.json?q=trek&apikey=3...
Out of the resulting 151 (!) movies, [ 18 ] if I select Star Trek Into Darkness , I get a JSON
object that looks like the following (with a lot of parts elided):
18 Including one called, I kid you not, Star Trek versus Batman . The Enterprise goes back in time to the 1960s and
gets taken over by the Joker and Catwoman. Seriously.
 
 
 
Search WWH ::




Custom Search