Close

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.

Example

Domain object

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

Controller

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;

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

    @Autowired
    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);
        resource.add(selfLink);
        return resource;
    }

    @GetMapping
    public Resources<Employee> getEmployees() {
        Link selfLink = entityLinks.linkToCollectionResource(Employee.class);
        List<Employee> employeeList = getEmployeeList();
        Resources<Employee> resources = new Resources<>(employeeList);
        resources.add(selfLink);
        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);
    }
}

JavaConfig

@EnableWebMvc
@ComponentScan
@Configuration
@EnableHypermediaSupport(type = EnableHypermediaSupport.HypermediaType.HAL)
public class AppConfig {
}

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:

{  
   "employeeId":1,
   "name":"Lara",
   "dept":"Admin",
   "salary":3000,
   "_links":{  
      "self":{  
         "href":"http://localhost:8080/employees/1"
      }
   }
}

Accessing http://localhost:8080/employees

{
   "_embedded":{
      "employeeList":[
         {
            "employeeId":1,
            "name":"Lara",
            "dept":"Admin",
            "salary":3000
         },
         {
            "employeeId":2,
            "name":"Tom",
            "dept":"IT",
            "salary":4000
         }
      ]
   },
   "_links":{
      "self":{
         "href":"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
                • EmployeeController.java

    See Also