接收端:
ParameterizedTypeReference<Page<MyObject>> responseType = new ParameterizedTypeReference<Page<MyObject>>() { };ResponseEntity<Page<MyObject>> result = restTemplate.exchange(url, HttpMethod.GET, null/*httpEntity*/, responseType);List<MyObject> searchResult = result.getBody().getContent();
ps:其实还有一个方法:把Page换成map,用map保存必要数据然后返回,虽然很low的感觉,不过还是写下来了
数据查询端:
接收的时候改成:
ParameterizedTypeReference<Page<MyObject>> responseType = new ParameterizedTypeReference<Page<MyObject>>() { };ResponseEntity<Page<MyObject>> result = restTemplate.exchange(url, HttpMethod.GET, null/*httpEntity*/, responseType);List<MyObject> searchResult = result.getBody().getContent();
异常:
org.springframework.http.converter.HttpMessageNotReadableException: Could not read document: Can not construct instance of org.springframework.data.domain.Page,
problem: abstract types either need to be mapped to concrete types, have custom deserializer, or be instantiated with additional type information at [Source: java.io.PushbackInputStream@3be1e1f2; line: 1, column: 1]; nested exception is com.fasterxml.jackson.databind.JsonMappingException: Can not construct instance of org.springframework.data.domain.Page, problem: abstract types either need to be mapped to concrete types, have custom deserializer, or be instantiated with additional type information
查了好久找到了解决方案:
总结下来,是三种方案:1、自定义接收数据模型(属性要和page传递数据一致)
2、自定义类实现Page接口
3、自定义类继承PageImpl类(此类为Page接口实现类)
代码:
1、自定义接收数据类型
第二种和第三种可以在这个网址看到: https://stackoverflow.com/questions/34647303/spring-resttemplate-with-paginated-api
ps:其实还有一个方法:把Page换成map,用map保存必要数据然后返回,虽然很low的感觉,不过还是写下来了
数据查询端:
接收的时候改成:
本文介绍了解决Spring框架中Page对象数据转换异常的具体方法,包括自定义接收数据模型、自定义类实现Page接口和继承PageImpl类等方案。
4万+

被折叠的 条评论
为什么被折叠?



