ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

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

2022-02-22 11:32:53  阅读:299  来源: 互联网

标签:Feign total return public content Returns 报错 序列化 page


自己在写项目的时候,调用JPA接口去查询数据一直报错,打断点又是可以得到数据的,只是接收不了,百度了很久,这个方法是可以进行解决的

报错:

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]

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

问题就出在这个PageImpl对象, 正常状况下没有任何问题, 可是若是这个对象经过Feign中转时, 就会出现没法反序列化的错误.app

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

解决办法:有两种,第一种是自定义反序列化,又麻烦又不会。。。第二种就是重新写一个实现类实现Page,然后在feign的调用方(也就是API接口的地方)用这个类来接受数据,而不是用自带的Page类,这样就可以得到数据啦

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();  
    }  
} 

标签:Feign,total,return,public,content,Returns,报错,序列化,page
来源: https://blog.csdn.net/tznb1996/article/details/123042888

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有