springhateoasspring-hateoas

Proper way to convert Spring HATEOAS Link to Object


I have a very simple controller the makes an HTTP request and receives some resources in HATEOAS format.

package com.provider.spring.controller;

import java.util.List;

import org.springframework.hateoas.Link;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.client.RestTemplate;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.provider.employee.EmployeeDTO;
import com.provider.http.HttpComponentsClientHttpRequestFactoryBasicAuth;
import com.provider.spring.rest.Resource;

@Controller
public class EmployeeController {

    private static final String REL_SELF = "self";
    private static final String REL_SEARCH = "search";
    private static final String REL_EMPLOYEE = "employee";
    private static final String RESOURCE_URI = "http://localhost:8080/employees";

    private RestTemplate restTemplate = new RestTemplate(new HttpComponentsClientHttpRequestFactoryBasicAuth("user", "password"));
    private List<EmployeeDTO> employees;


    @RequestMapping("/employees")
    public String getAllEmployees() {

        String result = null;
        try {
            String resultBody = restTemplate.getForObject(RESOURCE_URI, String.class);
            ObjectMapper objectMapper = new ObjectMapper();
            Resource<EmployeeDTO> resource = objectMapper.readValue(resultBody, Resource.class);

            // Get objects with relation "employee"
            for(Link l : resource.getLinks()) {
                if(l.getRel().equals(REL_EMPLOYEE)) {
                    // TODO: Construct EmployeeDTO from Link.
                    // TODO: Add EmployeeDTO to list.
                }
            }
        }
        catch(Exception e) {
            e.printStackTrace();
            result = "error";
            return result;
        }
        return result;
    }
}

Is there an existing prefered or standard way of converting Links to Objects?

See here for details on the EmployeeDTO: https://gist.github.com/Xerosigma/64469a30355f5de0228a


Solution

  • A couple ways to do this with Jackson:

    Without custom converter:

    ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.registerModule(new Jackson2HalModule());
    RestTemplate restTemplate= new RestTemplate();
    
    String resultBody = restTemplate.getForObject(link.getHref(), String.class);
    EmployeeDTO resource = objectMapper.readValue(resultBody, EmployeeDTO.class);
    

    With custom converter:

    ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.registerModule(new Jackson2HalModule());
    RestTemplate restTemplate= new RestTemplate();
    
    MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
    converter.setObjectMapper(objectMapper);
    restTemplate.setMessageConverters(Collections.<HttpMessageConverter<?>> singletonList(converter));
    EmployeeDTO resource = restTemplate.getForObject(link.getHref(), EmployeeDTO.class);