new TypeReference
该语句的问题在于,Jackson无法实例化抽象类型。您应该为Jackson提供有关如何实例化
Page具体类型的信息。但是它的具体类型,
PageImpl没有默认构造函数或与此相关的任何
@JsonCreator,因此您也
不能 使用以下代码:
new TypeReference<PageImpl<StoryResponse>>() {}
由于您不能将所需的信息添加到
Page类中,因此最好为
Page具有默认no-
arg构造函数的接口创建一个自定义实现,如本[答案所示](http://codingdict.com/questions/110311。然后在类型引用中使用该自定义实现,如下所示:
new TypeReference<CustomPageImpl<StoryResponse>>() {}
这是从链接的问题中复制的自定义实现:
public class CustomPageImpl<T> extends PageImpl<T> { private static final long serialVersionUID = 1L; private int number; private int size; private int totalPages; private int numberOfElements; private long totalElements; private boolean previousPage; private boolean firstPage; private boolean nextPage; private boolean lastPage; private List<T> content; private Sort sort; public CustomPageImpl() { super(new ArrayList<>()); } @Override public int getNumber() { return number; } public void setNumber(int number) { this.number = number; } @Override public int getSize() { return size; } public void setSize(int size) { this.size = size; } @Override public int getTotalPages() { return totalPages; } public void setTotalPages(int totalPages) { this.totalPages = totalPages; } @Override public int getNumberOfElements() { return numberOfElements; } public void setNumberOfElements(int numberOfElements) { this.numberOfElements = numberOfElements; } @Override public long getTotalElements() { return totalElements; } public void setTotalElements(long totalElements) { this.totalElements = totalElements; } public boolean isPreviousPage() { return previousPage; } public void setPreviousPage(boolean previousPage) { this.previousPage = previousPage; } public boolean isFirstPage() { return firstPage; } public void setFirstPage(boolean firstPage) { this.firstPage = firstPage; } public boolean isNextPage() { return nextPage; } public void setNextPage(boolean nextPage) { this.nextPage = nextPage; } public boolean isLastPage() { return lastPage; } public void setLastPage(boolean lastPage) { this.lastPage = lastPage; } @Override public List<T> getContent() { return content; } public void setContent(List<T> content) { this.content = content; } @Override public Sort getSort() { return sort; } public void setSort(Sort sort) { this.sort = sort; } public Page<T> pageImpl() { return new PageImpl<>(getContent(), new PageRequest(getNumber(), getSize(), getSort()), getTotalElements()); }}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)