Type definition error: [simple type, class org.springframework.data.domain.Page];
nested exception is com.fasterxml.jackson.databind.exc.InvalidDefinitionException:
Cannot construct instance of `org.springframework.data.domain.Page` (no Creators, like default construct, exist):
abstract types either need to be mapped to concrete types, have custom deserializer, or contain additional type information
at [Source: (PushbackInputStream); line: 1, column: 48] (through reference chain:
com.core.domain.dto.ResultDTO["data"]->com.trade.manager.rest.search.domain.dto.PCShopSearchProductDTO["products"]), feign.codec.DecodeException
feign.codec.DecodeException:
Type definition error:
[simple type, class org.springframework.data.domain.Page]; nested exception is com.fasterxml.jackson.databind.exc.InvalidDefinitionException:
Cannot construct instance of `org.springframework.data.domain.Page` (no Creators, like default construct, exist):
abstract types either need to be mapped to concrete types, have custom deserializer, or contain additional type information
at [Source: (PushbackInputStream); line: 1, column: 48] (through reference chain:
com.core.domain.dto.ResultDTO["data"]->com.trade.manager.rest.search.domain.dto.PCShopSearchProductDTO["products"])
at feign.SynchronousMethodHandler.decode(SynchronousMethodHandler.java:180) ~[feign-core-10.1.0.jar!/:na]
at feign.SynchronousMethodHandler.executeAndDecode(SynchronousMethodHandler.java:140) ~[feign-core-10.1.0.jar!/:na]
at feign.SynchronousMethodHandler.invoke(SynchronousMethodHandler.java:78) ~[feign-core-10.1.0.jar!/:na]
at feign.ReflectiveFeign$FeignInvocationHandler.invoke(ReflectiveFeign.java:103) ~[feign-core-10.1.0.jar!/:na]
at com.sun.proxy.$Proxy161.searchProductShopPC(Unknown Source) ~[na:na]

Spring Data JPA的Page接口, 定义了分页的基本操作, 用起来很是方便. Page接口在SimpleJpaRepository中用得比较广泛, 例如: org.springframework.data.jpa.repository.support.SimpleJpaRepository#findAll

@Override
public <S extends T> Page<S> findAll(Example<S> example, Pageable pageable) {
ExampleSpecification<S> spec = new ExampleSpecification<S>(example);
Class<S> probeType = example.getProbeType();
TypedQuery<S> query = getQuery(new ExampleSpecification<S>(example), probeType, pageable);
return pageable == null ? new PageImpl<S>(query.getResultList()) : readPage(query, probeType, pageable, spec);
}

该查询返回一个Page实例, 具体的实现是org.springframework.data.domain.PageImpl.

问题就出在这个PageImpl对象, 正常情况下没有任何问题, 但是如果这个对象通过Feign中转时, 就会出现无法反序列化的错误.

究其原因, 是PageImpl没有无参构造, 其超类Chunk也没有无参构造; 导致反序列化失败.

解决的方法有两种, 一是自定义反序列化, 比较麻烦.
另一种办法就是自定义Page, 放弃Spring自带的PageImpl, 这就解决了反序列化的问题. 笔者使用的是后一种方法.
这是笔者自定义的Page实现, 与Spring无关, 可以在任何项目中通用:

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List; /**
* Page operations.
*
* @auther rickgong@iteye.com on 2017/3/17.
* @see org.springframework.data.domain.Page
*/
public class Page<T> implements Iterable<T>, Serializable {
private static final long serialVersionUID = -3720998571176536865L;
private List<T> content = new ArrayList<>();
private long total;
private int pageNo;
private int pageSize; public Page() {
} public Page(List<T> content, long total, int pageNo, int pageSize) {
this.content = content;
this.total = total;
this.pageNo = pageNo;
this.pageSize = pageSize;
} /**
* Returns if there is a previous page.
*
* @return if there is a previous page.
*/
public boolean hasPrevious() {
return getPageNo() > 0;
} /**
* Returns if there is a next page.
*
* @return if there is a next page.
*/
public boolean hasNext() {
return getPageNo() + 1 < getTotalPage();
} /**
* Returns whether the current page is the first one.
*
* @return whether the current page is the first one.
*/
public boolean isFirst() {
return !hasPrevious();
} /**
* Returns whether the current page is the last one.
*
* @return whether the current page is the last one.
*/
boolean isLast() {
return !hasNext();
} /**
* Returns the total amount of elements of all pages.
*
* @return the total amount of elements of all pages.
*/
public long getTotal() {
return total;
} public void setTotal(long total) {
this.total = total;
} /**
* Returns the number of total pages.
*
* @return the number of total pages
*/
public int getTotalPage() {
return getPageSize() == 0 ? 1 : (int) Math.ceil((double) total / (double) getPageSize());
} /**
* Returns the page content as unmodifiable {@link List}.
*
* @return Returns the page content as unmodifiable {@link List}
*/
public List<T> getContent() {
return Collections.unmodifiableList(content);
} public void setContent(List<T> content) {
this.content = content;
} /**
* Returns whether the current page has content.
*
* @return whether the current page has content.
*/
public boolean hasContent() {
return getContentSize() > 0;
} /**
* Returns the number of elements on current page.
*
* @return the number of elements on current page.
*/
public int getContentSize() {
return content.size();
} /**
* Returns the number of items of each page.
*
* @return the number of items of each page
*/
public int getPageSize() {
return pageSize;
} public void setPageSize(int pageSize) {
this.pageSize = pageSize;
} /**
* Returns the number of current page. (Zero-based numbering.)
*
* @return the number of current page.
*/
public int getPageNo() {
return pageNo;
} /**
* Set the number of current page. (Zero-based numbering.)
*/
public void setPageNo(int pageNo) {
this.pageNo = pageNo;
} @Override
public Iterator<T> iterator() {
return getContent().iterator();
}
}

https://www.iteye.com/blog/rickgong-2363789

Spring Data JPA整合REST客户端Feign时: 分页查询的反序列化报错的问题的更多相关文章

  1. Spring Data JPA 整合Spring 第二篇

    主要是在CustomerDao中去写一些代码,在调用Query中去用SQL 例如 public interface CustomerDao extends JpaRepository<Custo ...

  2. Spring Data JPA 整合Spring

    1.1   Spring Data JPA 与 JPA和hibernate之间的关系 JPA是一套规范,内部是有接口和抽象类组成的.hibernate是一套成熟的ORM框架,而且Hibernate实现 ...

  3. Spring Data JPA基本增删改查和JPQL查询(含完整代码和视频连接)

    问题:SpringDataJPA怎么使用? 一.考察目标 主要考核SpringDataJPA的用法 二.题目分析 spring data jpa 的使用步骤(下面有具体实现细节) 1.创建maven工 ...

  4. Spring Data Jpa+SpringMVC+Jquery.pagination.js实现分页

    本博客介绍基于Spring Data这款orm框架加上Jquery.pagination插件实现的分页功能. 介绍一下Spring Data框架 spring Data : Spring 的一个子项目 ...

  5. 【spring data jpa】 spring data jpa 中 时间格式设置between and 查询

    实例代码: //举报时间 Date createDate = entity.getCreateDate(); if (createDate != null){ predicates.add(cb.be ...

  6. 展开被 SpringBoot 玩的日子 《 五 》 spring data jpa 的使用

    在上篇文章< 展开被 SpringBoot 玩的日子 < 二 >WEB >中简单介绍了一下spring data jpa的基础性使用,这篇文章将更加全面的介绍spring da ...

  7. spring data jpa 全面解析(实践 + 源码分析)

    前言 本文将从示例.原理.应用3个方面介绍spring data jpa. 以下分析基于spring boot 2.0 + spring 5.0.4版本源码 概述 JPA是什么? JPA (Java ...

  8. springboot:spring data jpa介绍

    转载自:https://www.cnblogs.com/ityouknow/p/5891443.html 在上篇文章springboot(二):web综合开发中简单介绍了一下spring data j ...

  9. spring boot(五)Spring data jpa介绍

    在上篇文章springboot(二):web综合开发中简单介绍了一下spring data jpa的基础性使用,这篇文章将更加全面的介绍spring data jpa 常见用法以及注意事项 使用spr ...

随机推荐

  1. ORB-SLAM2 地图加载

    一.前面说了ORB-SLAM地图的保存部分,继续说地图如何加载,因为加载部分相比保存要稍微复杂一些,所以要多说一点. 二.ORB-SLAM2地图加载构成 首先同样是在头文件中声明加载函数,包含地图点和 ...

  2. 剑指offer:按之字形打印二叉树(栈|双向队列+中序遍历)

    1. 题目描述 /** 请实现一个函数按照之字形打印二叉树, 即第一行按照从左到右的顺序打印, 第二层按照从右至左的顺序打印, 第三行按照从左到右的顺序打印, 其他行以此类推. */ 2. 双向队列 ...

  3. [Codeforces1250E] The Coronation

    [Codeforces1250E] The Coronation The Coronation 又是一道并查集...最近做的并查集咋这么多... 思路 首先,维护元素间关系的题想到并查集. 因为这里涉 ...

  4. 解决 ubuntu 开机卡死在输入密码界面 && 键盘鼠标失灵!!

    近期不知安装了什么package,导致 ubuntu 开机后键盘鼠标一直没法用,刚开始以为是 ubuntu 桌面环境崩溃了,后来发现系统能显示连接到网络.时间也在运行,那应该就是键盘鼠标失灵了. 网上 ...

  5. 《一起学netty》

    o文章摘自 netty 官网(netty.io)   netty 是一个异步的,事件驱动的网络应用通信框架,可以让我们快速编写可靠,高性能,高可扩展的服务端和客户端   样例一:discard ser ...

  6. 记录几篇PM文章

    今日阅读了几篇关于 PM 的文章感觉还不错,整理.摘录于此: 1.[项目经理和产品经理:https://www.cnblogs.com/joylee/p/3541141.html] ——关于二者的异同 ...

  7. C#中文转换为拼音NPinyin代码【转】

    项目地址:https://code.google.com/p/npinyin/ 在一个采集的程序中,为了给每个文章起一个别名,据说有很好的别名的话,对于百度.google的收录 是很有好处的.按照Se ...

  8. Logstash:处理多个input

    Logstash:处理多个input Logstash的整个pipleline分为三个部分: input插件:提取数据. 这可以来自日志文件,TCP或UDP侦听器,若干协议特定插件(如syslog或I ...

  9. 对try catch finally的理解

    对try catch finally的理解1.finally  总是会运行的,即使在catch中thorw抛出异常了. 2.finally 在 return后没有结束,而是继续运行finally 2. ...

  10. E203 译码模块(2)

    常用的alu算术运算指令(包括ecall和 ebreak)在regular alu单元处理.regular alu单元为alu单元的一个子单元.regular单元的信息总线共21位,格式如下图所示,其 ...