Java Reference
In-Depth Information
Offering
Different
Representations
of
the
Same
Resource
Problem
You want your service to provide alternate views of the same resource.
Solution
Provide separate methods that respond to the HTTP protocol you're interested in (e.g., mul-
tiple methods with a
@GET
annotation) and specify a different
@Produces
for each one that
indicates the MIME type it returns.
Discussion
A standard browser will pass the MIME types it can handle by passing the HTTP
Accept
header in its request. A typical request header sent from Firefox looks like this:
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
The client is telling the server that it would prefer to get HTML or XHTML, and its next pref-
erence is to accept XML. The preferences are indicated not only by the order in which they
appear, but with the
q
parameter, used to specify a relative “quality factor” or how strong the
preference is for the given media type on a scale of 0 to 1.
NOTE
So to test the client, you'll need to indicate an alternative HTTP
Accept
header. Building a
client programmatically using something like the HTTP Commons Client library will do this
for you. To run this example, you'll also need Apache Commons Logging, Logging API, and
Codec on your classpath, as the Apache HTTP client requires them.