Deserialize JSON containing (_links and _embedded) using spring-hateoas
Finally, I found a better a way to consume those application/hal+json APIs.
Spring hateoas actually provides a client almost ready to use: org.springframework.hateoas.client.Traverson.
Traverson traverson = new Traverson(new URI("http://localhost:8080/test"), MediaTypes.HAL_JSON);
TraversalBuilder tb = traverson.follow("users");
ParameterizedTypeReference<Resources<UserJson>> typeRefDevices = new ParameterizedTypeReference<Resources<UserJson>>() {};
Resources<UserJson> resUsers = tb.toObject(typeRefDevices);
Collection<UserJson> users= resUsers .getContent();
As you can see, I got rid UsersJson and UsersEmbeddedListJson.
Here are the maven dependencies I added
<dependency>
<groupId>org.springframework.hateoas</groupId>
<artifactId>spring-hateoas</artifactId>
<version>0.19.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.plugin</groupId>
<artifactId>spring-plugin-core</artifactId>
<version>1.2.0.RELEASE</version>
</dependency>
<dependency>
<groupId>com.jayway.jsonpath</groupId>
<artifactId>json-path</artifactId>
<version>2.0.0</version>
</dependency>
Had to add this to my DTO:
@JsonProperty("_links")
public void setLinks(final Map<String, Link> links) {
links.forEach((label, link) -> add(link.withRel(label)) );
}
since ResourceSupport has no POJO standard/Json-signaled setter/constructor for links