Chapter 3. RESTful Web Services: The Client Side

Consider a familiar scenario in which a client accesses a RESTful web service:

  1. The client issues a GET request against a RESTful service. In general, a request to a RESTful service targets a named resource, with a URI as the name.
  2. If successful, this GET request results in a response document, a representation of the resource targeted in the request. The document is usually in XML or JSON, although other formats might be available. In any case, the response should be MIME-typed.
  3. The client extracts from the document whatever information is required for the client’s application logic.

For example, the client might be a jQuery script embedded in an HTML document. This client requests data, such as a product list, from a web service. If the request succeeds, the client extracts relevant information; for instance, the client might extract the name, category, price, and in-stock quantity of each product in the list. The client then processes the information in some appropriate way; for example, the JavaScript client might build and display an HTML table that represents the product list, with one row per product.

Each step in this sample scenario poses a challenge. The GET request must be formatted properly, and the status code of the response should be inspected to determine whether the request succeeded and, if not, why not. The toughest challenge for the client, however, may be extracting required information from the document: the ...

Get Java Web Services: Up and Running, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.