数据查询端:
Page hosts=service.listByXX(MyObject);
接收端:
ParameterizedTypeReference
ResponseEntity
List
异常:
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、自定义接收数据类型
public class PageResultModel { private int number; private int size; private List content; private boolean last; private int totalPages; private int totalElements; private Object sort; private boolean first; private int numberOfElements; private boolean next; private boolean previous; public int getNumber() {return number;} public void setNumber(int number) {this.number = number;} public int getTotalPages() {return totalPages;} public void setTotalPages(int totalPages) {this.totalPages = totalPages;} public int getNumberOfElements() {return numberOfElements;} public void setNumberOfElements(int numberOfElements) {this.numberOfElements = numberOfElements;} public boolean isFirst() {return first;} public void setFirst(boolean first) {this.first = first;} public int getTotalElements() {return totalElements;} public void setTotalElements(int totalElements) {this.totalElements = totalElements;} public Object getSort() {return sort;} public void setSort(Object sort) {this.sort = sort;} public boolean isLast() {return last;} public void setLast(boolean last) {this.last = last;} public int getSize() {return size;} public void setSize(int size) {this.size = size;} public List getContent() {return content;} public void setContent(List content) {this.content = content;} public boolean hasPrevious() { return previous; } public void setPrevious(boolean previous) { this.previous = previous; } public boolean hasNext() { return next; } public void setNext(boolean next) { this.next = next; } }
第二种和第三种可以在这个网址看到:https://stackoverflow.com/questions/34647303/spring-resttemplate-with-paginated-api
ps:其实还有一个方法:把Page换成map,用map保存必要数据然后返回,虽然很low的感觉,不过还是写下来了
数据查询端:
Page page=service.listByXX(myObject); map.put("data",page.getContent); map.put("totalSize",page.getTotalElements()); .....
接收的时候改成:
ParameterizedTypeReference