Back to Catalog

Building a hypermedia-driven RESTful Java microservice using Hypermedia as the Engine of Application State (HATEOAS)

BeginnerGuided Project

You’ll explore how to use Hypermedia As The Engine Of Application State (HATEOAS) to drive your RESTful web service on Open Liberty.

4.8 (10 Reviews)

Language

  • English

Topic

  • Open Liberty

Enrollment Count

  • 59

Skills You Will Learn

  • Java

Offered By

  • IBM

Estimated Effort

  • 30 minutes

Platform

  • SkillsNetwork

Last Update

  • April 29, 2024
About This Guided Project
You will learn how to use hypermedia to create a specific style of a response JSON, which has contents that you can use to navigate your REST service. You’ll build on top of a simple inventory REST service that you can develop with MicroProfile technologies. You can find the service at the following URL:
http://localhost:9080/inventory/hosts
The service responds with a JSON file that contains all of the registered hosts. Each host has a collection of HATEOAS links:
{
  "foo": [
    {
      "href": "http://localhost:9080/inventory/hosts/foo",
      "rel": "self"
    }
  ],
  "bar": [
    {
      "href": "http://localhost:9080/inventory/hosts/bar",
      "rel": "self"
    }
  ],
  "*": [
    {
      "href": "http://localhost:9080/inventory/hosts/*",
      "rel": "self"
    }
  ]
}

What is HATEOAS?
HATEOAS is a constrained form of REST application architecture. With HATEOAS, the client receives information about the available resources from the REST application. The client does not need to be hardcoded to a fixed set of resources, and the application and client can evolve independently. In other words, the application tells the client where it can go and what it can access by providing it with a simple collection of links to other available resources.

Response JSON
In the context of HATEOAS, each resource must contain a link reference to itself, which is commonly referred to as self. In this guide, the JSON structure features a mapping between the hostname and its corresponding list of HATEOAS links:
  "*": [
    {
      "href": "http://localhost:9080/inventory/hosts/*",
      "rel": "self"
    }
  ]

Link types
The following example shows two different links. The first link has a self relationship with the resource object and is generated whenever you register a host. The link points to that host entry in the inventory:
  {
    "href": "http://localhost:9080/inventory/hosts/<hostname>",
    "rel": "self"
  }
The second link has a properties relationship with the resource object and is generated if the host system service is running. The link points to the properties resource on the host:
  {
    "href": "http://<hostname>:9080/system/properties",
    "rel": "properties"
  }

Other formats
Although you should stick to the previous format for the purpose of this guide, another common convention has the link as the value of the relationship:
  "_links": {
      "self": "http://localhost:9080/inventory/hosts/<hostname>",
      "properties": "http://<hostname>:9080/system/properties"
  }