Close

Spring HATEOAS - Converting entities to resource types with ResourceAssembler

[Last Updated: Jul 20, 2018]

Spring HATEOAS provides ResourceAssembler interface to make it possible to use dedicated classes responsible for converting domain types to REST resource types. This helps us not to repeat resources and their links creation at multiple places in controller classes.

In this example we will extend ResourceAssemblerSupport (an implementation of ResourceAssembler) to understand how that works.

Example

Domain class

public class Employee {
  private Long id;
  private String name;
  private String dept;
  private int salary;
    .............
}

Resource class

package com.logicbig.example;

import org.springframework.hateoas.ResourceSupport;

public class EmployeeResource extends ResourceSupport {
  private String name;
  private String dept;
    .............
}

Implementation of ResourceAssembler

package com.logicbig.example;

import org.springframework.hateoas.mvc.ResourceAssemblerSupport;

public class EmployeeResourceAssembler extends ResourceAssemblerSupport<Employee, EmployeeResource> {
  public EmployeeResourceAssembler() {
      super(EmployeeController.class, EmployeeResource.class);
  }

  @Override
  public EmployeeResource toResource(Employee entity) {
      EmployeeResource er = super.createResourceWithId(entity.getId(), entity);
      er.setName(entity.getName());
      er.setDept(entity.getDept());
      return er;
  }
}

As seen above createResourceWithId() method allows us to create an instance of the resource and have a Link with a rel of self added to it. The href of that link is determined by the configured controller's request mapping (/employees/{employeeId} in our case).

Controller

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

  @GetMapping("/{employeeId}")
  public EmployeeResource getEmployeeById(@PathVariable long employeeId) {
      Employee employee = getEmployee(employeeId);
      return new EmployeeResourceAssembler()
              .toResource(employee);
  }

  private Employee getEmployee(long employeeId) {
      //todo: replace following with employee service
      return Employee.create(employeeId, "Tom", "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

$ curl -s http://localhost:8080/employees/1 | jq
{
"name" : "Tom",
"dept" : "Admin",
"_links" : {
"self" : {
"href" : "http://localhost:8080/employees/1"
}
}
}

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

ResourceAssembler Example Select All Download
  • spring-hateoas-resource-assembler
    • src
      • main
        • java
          • com
            • logicbig
              • example
                • EmployeeResourceAssembler.java

    See Also