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?