0

Using gs-accessing-data-rest-complete as the base, a list of people is generated in the following format at http://localhost:8080/people:

{
  "_links" : {
    "self" : {
      "href" : "http://localhost:8080/people{?page,size,sort}",
      "templated" : true
    },
    "search" : {
      "href" : "http://localhost:8080/people/search"
    }
  },
  "_embedded" : {
    "people" : [ {
      "firstName" : "Frodo",
      "lastName" : "Baggins",
      "_links" : {
        "self" : {
          "href" : "http://localhost:8080/people/1"
        }
      }
    }, {
      "firstName" : "Bilbo",
      "lastName" : "Baggins",
      "_links" : {
        "self" : {
          "href" : "http://localhost:8080/people/2"
        }
      }
    }, {
      "firstName" : "Bilbo Jr.",
      "lastName" : "Baggins",
      "_links" : {
        "self" : {
          "href" : "http://localhost:8080/people/3"
        }
      }
    } ]
  },
  "page" : {
    "size" : 20,
    "totalElements" : 3,
    "totalPages" : 1,
    "number" : 0
  }
}

How do I consume this REST data on the client?

Gaurav Sharma
  • 4,032
  • 14
  • 46
  • 72

1 Answers1

0

After a bunch of digging around, and trying the solution provided at Why does RestTemplate not bind response representation to PagedResources?, I found the most efficient way in Jackson2HalIntegrationTest.java. Here is my implementation:

package hello;

import java.io.IOException;
import java.net.URL;

import org.junit.Test;
import org.springframework.hateoas.Resource;
import org.springframework.hateoas.Resources;
import org.springframework.hateoas.hal.Jackson2HalModule;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;

public class PersonTest {

    private static final String URI_TEMPLATE = "http://localhost:8080/people";

    @Test
    public void p1() throws JsonParseException, JsonMappingException, IOException {

        ObjectMapper mapper = new ObjectMapper();
        mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES,
                false);
        mapper.registerModule(new Jackson2HalModule());

        URL url = new URL(URI_TEMPLATE);

        Resources<Resource<PersonResource>> result = mapper.readValue(url, mapper.getTypeFactory().constructParametricType(Resources.class,
                mapper.getTypeFactory().constructParametricType(Resource.class, PersonResource.class)));

        for (Resource<PersonResource> resource : result) {
            System.out.println(resource.getContent().getFirstName());
            System.out.println(resource.getContent().getLastName());            
            System.out.println(resource.getLink("self").getHref());
            System.out.println("=========================================");
        }

    }
}

and PersonResource.java

package hello;

import org.springframework.hateoas.core.Relation;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

@JsonIgnoreProperties(ignoreUnknown = true)
@Relation(value = "person", collectionRelation = "people")
public class PersonResource {

    private String firstName;
    private String lastName;


    public PersonResource() {

    }

    public PersonResource(String firstName, String lastName) {
        this.firstName = firstName;
        this.lastName = lastName;       
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result
                + ((firstName == null) ? 0 : firstName.hashCode());
        result = prime * result
                + ((lastName == null) ? 0 : lastName.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        PersonResource other = (PersonResource) obj;
        if (firstName == null) {
            if (other.firstName != null)
                return false;
        } else if (!firstName.equals(other.firstName))
            return false;
        if (lastName == null) {
            if (other.lastName != null)
                return false;
        } else if (!lastName.equals(other.lastName))
            return false;
        return true;
    }   
}
Community
  • 1
  • 1
Gaurav Sharma
  • 4,032
  • 14
  • 46
  • 72