[英]Marshalling Spring Data REST Query Results with RestTemplate
When using RestTemplate.exchange
to invoke a query endpoint, what return type should I expect? 使用
RestTemplate.exchange
调用查询端点时,我应该期望哪种返回类型?
My repository has a method thus: 我的存储库有一个方法可以这样:
Page<Account> findAccountByFirstName(String name, Pageable pageable);
In my integration test, I'm making the following call with a RestTemplate
: 在集成测试中,我使用
RestTemplate
进行以下调用:
ResponseEntity<List<Account>> accountResponse = restPleb.exchange(
new RequestEntity<Void>(HttpMethod.GET, getUri("/accounts/search/findByFirstName?firstName=Kevin")),
new ParameterizedTypeReference<List<Account>>() {}
);
The response has the list of found resources in an object called accounts
: 响应在名为
accounts
的对象中具有找到的资源的列表:
{
"_embedded" : {
"accounts" : [ {
"lastName" : "Lastname",
"firstName" : "Kevin",
"phoneNumber " : "+44 7700000000",
"email" : "kevin@example.com",
"_links" : {
"self" : {
"href" : "http://localhost:53826/accounts/id"
},
"persistableAccount" : {
"href" : "http://localhost:53826/accounts/id"
}
}
} ]
},
"_links" : {
"self" : {
"href" : "http://localhost:53826/accounts/search/findByFirstName?firstName=Kevin"
}
},
"page" : {
"size" : 20,
"totalElements" : 1,
"totalPages" : 1,
"number" : 0
}
}
I get the following error where between them RestTemplate
and Jackson can't figure out what to do with the response. 我收到以下错误,它们之间的
RestTemplate
和Jackson无法弄清楚该如何处理响应。 Presumably this is because it's expecting an array rather than an object. 大概是因为它期望一个数组而不是一个对象。
org.springframework.http.converter.HttpMessageNotReadableException: Could not read document: Can not deserialize instance of java.util.ArrayList out of START_OBJECT token
Is there any way of getting RestTemplate
to give the right hints to Jackson to deserialize this properly? 有没有办法让
RestTemplate
给Jackson正确的提示以正确地反序列化呢? I would have thought that there must be a way, being that it's Spring on both sides of the divide! 我本以为一定有办法,因为鸿沟在两边!
Update 更新资料
I've tried specifying the response type as Resources<Account>
, which prevents an exception being thrown gives an empty list instead: Resources { content: [], links: [] }
我尝试将响应类型指定为
Resources<Account>
,这可以防止引发异常,而是提供一个空列表: Resources { content: [], links: [] }
As per the format , it has to be parsed in a different manner is what I suppose. 按照格式 ,我想它必须以不同的方式解析。 Can this example be used to parse - defined here
可以使用此示例进行解析- 此处定义
Registering the converter to the template -> and using the resource to parse the response. 将转换器注册到模板->并使用资源来解析响应。
public RestTemplate getRestTemplateWithHalMessageConverter() {
RestTemplate restTemplate = new RestTemplate();
List<HttpMessageConverter<?>> existingConverters = restTemplate.getMessageConverters();
List<HttpMessageConverter<?>> newConverters = new ArrayList<>();
newConverters.add(getHalMessageConverter());
newConverters.addAll(existingConverters);
restTemplate.setMessageConverters(newConverters);
return restTemplate;
}
private HttpMessageConverter getHalMessageConverter() {
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.registerModule(new Jackson2HalModule());
MappingJackson2HttpMessageConverter halConverter = new TypeConstrainedMappingJackson2HttpMessageConverter(ResourceSupport.class);
halConverter.setSupportedMediaTypes(Arrays.asList(HAL_JSON));
halConverter.setObjectMapper(objectMapper);
return halConverter;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.