Spring HATEOAS - Creating links based on model types by using EntityLinks

[Last Updated: Jul 6, 2018]

The EntityLinks interface allows to automatically use a LinkBuilder based on the model types. Its methods linkToSingleResource() and inkToCollectionResource() create links pointing to a single resource or a collection resource respectively.

EntityLinks is available for dependency injection by activating @EnableEntityLinks in our Spring MVC configuration. Instead of that @EnableHypermediaSupport (tutorial) additionally enables this functionality as well.

Activating this functionality will cause all Spring MVC controllers being inspected for the @ExposesResourceFor annotation. This annotation exposes which model type the controller manages.


Domain object

public class Employee {
  private long employeeId;
  private String name;
  private String dept;
  private int salary;


package com.logicbig.example;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.hateoas.*;
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;
import java.util.List;

public class EmployeeController {

  private EntityLinks entityLinks;

  @GetMapping(value = "/{employeeId}")
  public Resource<Employee> getEmployeeById(@PathVariable long employeeId) {
      Link selfLink = entityLinks.linkToSingleResource(Employee.class, employeeId);
      Employee employee = getEmployee(employeeId);
      Resource<Employee> resource = new Resource<>(employee);
      return resource;

  public Resources<Employee> getEmployees() {
      Link selfLink = entityLinks.linkToCollectionResource(Employee.class);
      List<Employee> employeeList = getEmployeeList();
      Resources<Employee> resources = new Resources<>(employeeList);
      return resources;

  private List<Employee> getEmployeeList() {
      //todo: replace with employee service
      return List.of(getEmployee(1),
              Employee.create(2, "Tom", "IT", 4000));

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


@EnableHypermediaSupport(type = EnableHypermediaSupport.HypermediaType.HAL)
public class AppConfig {


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

mvn tomcat7:run-war


Accessing http://localhost:8080/employees/1:


Accessing http://localhost:8080/employees


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
  • spring-plugin-core 1.2.0.RELEASE: Core plugin infrastructure.
  • 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

EntityLinks example. Select All Download
  • spring-hateoas-entity-links
    • src
      • main
        • java
          • com
            • logicbig
              • example

    See Also