Close

Spring HATEOAS - Creating links using ResourceSupport and ControllerLinkBuilder

[Last Updated: Mar 20, 2020]

Spring HATEOAS module provides ResourceSupport class which allows to add hypermedia links with the web responses. We need to extend our POJOS from ResourceSupport.

The module also provides LinkBuilder interface to ease building Link instances. ControllerLinkBuilder is one of the implementation of this interface, which helps building Link instances pointing to Spring MVC controllers.

Example

In this example we will produce responses in JSON.

Maven dependencies

pom.xml

<dependency>
   <groupId>org.springframework.hateoas</groupId>
   <artifactId>spring-hateoas</artifactId>
   <version>0.24.0.RELEASE</version>
</dependency>
<dependency>
   <groupId>com.fasterxml.jackson.core</groupId>
   <artifactId>jackson-databind</artifactId>
   <version>2.9.5</version>
</dependency>

Resource Object

package com.logicbig.example;

import org.springframework.hateoas.ResourceSupport;

public class Employee extends ResourceSupport {
  private long employeeId;
  private String name;
  private String dept;
  private int salary;
    .............
}

MVC controller

package com.logicbig.example;

import org.springframework.hateoas.Link;
import org.springframework.hateoas.LinkBuilder;
import org.springframework.hateoas.mvc.ControllerLinkBuilder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/employees")
public class EmployeeController {

  @GetMapping("/{employeeId}")
  public Employee getEmployeeById(@PathVariable long employeeId) {
      Employee employee = getEmployee(employeeId);
      Link selfLink = ControllerLinkBuilder.linkTo(EmployeeController.class)
                                       .slash(employee.getEmployeeId())
                                       .withSelfRel();
      employee.add(selfLink);
      return employee;
  }

  private Employee getEmployee(long employeeId) {
      //todo: replace following with employee service
      return Employee.create(employeeId, "Tom", "Admin", 3000);
  }
}

Running

To try examples, run embedded tomcat (configured in pom.xml of example project below):

mvn tomcat7:run-war

Output

Accessing http://localhost:8080/employees/1 in a web browser will return:

{
"employeeId": 1,
"name": "Tom",
"dept": "Admin",
"salary": 3000,
"links": [
{
   "rel": "self",
   "href": "http://localhost:8080/employees/1",
   "hreflang": null,
   "media": null,
   "title": null,
   "type": null,
   "deprecation": null
}
]}

Example Project

Dependencies and Technologies Used:

  • spring-hateoas 0.24.0.RELEASE: Library to support implementing representations for hyper-text driven REST web services.
    Uses org.springframework:spring-web-mvc version 4.3.12.RELEASE
  • jackson-databind 2.9.5: General data-binding functionality for Jackson: works on core streaming API.
  • javax.servlet-api 3.0.1 Java Servlet API
  • JDK 10
  • Maven 3.5.4

ResourceSupport and ControllerLinkBuilder Example Select All Download
  • spring-hateoas-resource-support
    • src
      • main
        • java
          • com
            • logicbig
              • example
                • EmployeeController.java

    See Also