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.


In this example we will produce responses in JSON.

Maven dependencies



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;

public class EmployeeController {

  public Employee getEmployeeById(@PathVariable long employeeId) {
      Employee employee = getEmployee(employeeId);
      Link selfLink = ControllerLinkBuilder.linkTo(EmployeeController.class)
      return employee;

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


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

mvn tomcat7:run-war


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

    See Also