Chapter 6. Implementing HATEOAS

This chapter contains the following recipes:

  • Turning DTOs into Spring HATEOAS resources
  • Building links for a Hypermedia-driven API
  • Choosing a strategy to expose JPA entities
  • Retrieving data from a third-party API with OAuth

Introduction

What is HATEOAS? If you have never seen this word before, it can appear to be difficult to pronounce. Some pronounce it hate-ee-os; others say hate O-A-S. The important point is to remember that this abbreviation stands for Hypermedia as the Engine of Application State (HATEOAS). At the very least, you should remember Hypermedia. Hypermedia as a resource's capability to embed nodes that target external resources. Being connected to other resources, a hypermedia resource is also constrained to its domain, as it can't technically develop (as part of itself) other resources' domains.

Think of it as Wikipedia. If we create a page whose sections are not self contained in the page title (domain), and if one of these sections is already covered in an external page, there are few chances that this situation will be raised by an administrator.

HATEOAS is a constraint applicable to a REST architecture. It imposes on its resources a domain consistency, and at the same time, it imposes an explicit self documentation that the owner has to maintained for the sake of the whole cohesion.

The Richardson Maturity Model

The Richardson Maturity Model (by Leonard Richardson) provides a way to grade and qualify a REST API by its level of REST constraints:

The Richardson Maturity Model

The more REST-compliant an API is, the higher its grade.

The initial state in this model is Level 0: The Swamp of POX. Here, the protocol (usually HTTP) is only used for its transport capabilities (not for its state description features). Also, there are no resource-specific URIs here, just one endpoint is used for one method (normally, POST in HTTP).

Level 1: Resources is characterized by the implementation of resource-specific URIs. The resource identifiers can be found in URIs. However, still, only one method of the protocol is used (POST for HTTP again).

Level 2: HTTP Verbs reflects an improved use of the protocol properties. For HTTP, this actually means that the API is making use of the HTTP methods for their purpose (GET to read, POST to create, PUT to edit, DELETE to delete, and so on). Also, the API provides response codes that reliably inform the user about the operation state.

Level 3: Hypermedia Controls is the highest level in this model. It indicates the use of HATEOAS, which provides API-discovery features to the client.

You can read more on the Richardson Maturity Model on Martin Fowler's blog at:

http://martinfowler.com/articles/richardsonMaturityModel.html

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset