我正在尝试调用返回这种形式的数据的非常简单的 json 网络服务:
{
"_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
}
}
如您所见,它非常简单。 我解析链接没有问题,我的 POJO 扩展了 ResourceSupport。 以下是它们的样子:
UsersJson(根元素)
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 ...]
}
问题是我希望 jackson 和 spring 足够聪明,可以解析 _embedded 属性并填充我的 UsersJson.users 属性,但事实并非如此。
我尝试了在互联网上找到的各种方法,但唯一能让我正常工作的方法是创建一个充当 _embedded 包装器的新类:
UsersJson(根元素)
public class UsersJson extends ResourceSupport {
@JsonProperty("_embedded")
private UsersEmbeddedListJson embedded;
[... getters and setters ...]
}
嵌入式“包装器”
public class UsersEmbeddedListJson extends ResourceSupport {
private List<UserJson> users;
[... getters and setters ...]
}
它有效,但我觉得它很丑。
虽然我认为 RestTemplate 的以下配置会起作用(尤其是当我在 Jackson2HalModule 中看到 EmbeddedMapper 时),但它没有:
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);
谁能告诉我我错过了什么?
最佳答案
最后,我找到了一种更好的方式来使用那些 application/hal+json API。
Spring hateoas 实际上提供了一个几乎可以使用的客户端: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();
如你所见,我去掉了 UsersJson 和 UsersEmbeddedListJson。
这是我添加的 maven 依赖项
<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>
关于java - 使用 spring-hateoas 反序列化包含(_links 和 _embedded)的 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37813797/