Deserialize JSON containing (_links and _embedded) using spring-hateoas
Asked Answered
B

3

22

I am trying to invoque very simple json webservices that return data of this form:

{
    "_embedded": {
        "users": [{
            "identifier": "1",
            "firstName": "John",
            "lastName": "Doe",
            "_links": {
                "self": {
                    "href": "http://localhost:8080/test/users/1"
                }
            }
        },
        {
            "identifier": "2",
            "firstName": "Paul",
            "lastName": "Smith",
            "_links": {
                "self": {
                    "href": "http://localhost:8080/test/users/2"
                }
            }
        }]
    },
    "_links": {
     "self": {
       "href": "http://localhost:8080/test/users"
     }
   },
   "page": {
     "size": 20,
     "totalElements": 2,
     "totalPages": 1,
     "number": 0
   }
}

As you can see, it is pretty straight forward. I have no problems parsing the links, having my POJOs extending form ResourceSupport. Here is what they look like:

UsersJson (the root element)

public class UsersJson extends ResourceSupport {
    private List<UserJson> users;

    [... getters and setters ...]
}

UserJson

public class UserJson extends ResourceSupport {

    private Long identifier;

    private String firstName;

    private String lastName;

    [... getters and setters ...]
}

The thing is that I was expecting jackson and spring to be smart enough to parse the _embedded property and populate my UsersJson.users attribute but it isn't.

I tried various things I found on the internet but the only thing I could get to work properly was to create a new class acting as an _embedded wrapper:

UsersJson (the root element)

public class UsersJson extends ResourceSupport {
    @JsonProperty("_embedded")
    private UsersEmbeddedListJson  embedded;

    [... getters and setters ...]
}

Embedded "wrapper"

public class UsersEmbeddedListJson extends ResourceSupport {
    private List<UserJson> users;

    [... getters and setters ...]
}

It works but I find it quite ugly.

Yet I though the following configuration of the RestTemplate would have worked (especially when I saw EmbeddedMapper in Jackson2HalModule), but it didn't:

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

        MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
        converter.setSupportedMediaTypes(MediaType.parseMediaTypes("application/hal+json"));
        converter.setObjectMapper(mapper);

        RestTemplate restTemplate = new RestTemplate(Collections.singletonList(converter));

        ResponseEntity<UsersJson> result = restTemplate.getForEntity("http://localhost:8089/test/users", UsersJson.class, new HashMap<String, Object>());
        System.out.println(result);

Can somebody tell me what I am missing?

Benghazi answered 14/6, 2016 at 13:41 Comment(1)
It doesn't look like Jackson supports HAL _embedded links by default. Your wrapper looks like a good approach. It is also worthwhile taking a look at the Spring-HATEOAS @EnableHypermediaSupport annotation, but I haven't used it, so don't know if it will be useful.Douala
B
12

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>
Benghazi answered 1/7, 2016 at 14:16 Comment(1)
I am facing the same problem as you. When I tried your suggestion of using restTemplate.follow("users") then I get an error like Expected to find link with rel 'users' in response. In some sense this error make sense as follow api I assume is expecting it to point to some URL but in this case it is not. Not sure if I did something wrong hereBarocchio
I
5

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

Investigator answered 2/11, 2017 at 16:52 Comment(0)
R
0

I had to extend my DTO POJO with a custom setter parsing the produced output of Spring Hateos in the REST client side by using jackson as mapper:

HREF = "href" and REL is "rel" as constants.

@JsonProperty("link")
public void setLink(final List<Map<String, String>> links) {
    if (links != null) {
        links.forEach(l ->
                {
                    final String[] rel = new String[1];
                    final String[] href = new String[1];
                    l.entrySet().stream().filter((e) -> e.getValue() != null)
                            .forEach(e -> {
                                if (e.getKey().equals(REL)) {
                                    rel[0] = e.getValue();
                                }
                                if (e.getKey().equals(HREF)) {
                                    href[0] = e.getValue();
                                }
                            });
                    if (rel[0] != null && href[0] != null) {
                        add(new Link(href[0], rel[0]));
                    }
                }
        );
    }
}
Rhizo answered 20/6, 2018 at 2:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.