Spring Data JPA整合REST客戶端Feign時: 分頁查詢的反序列化報錯的問題

 

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#findAlljava

@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.spring

問題就出在這個PageImpl對象, 正常狀況下沒有任何問題, 可是若是這個對象經過Feign中轉時, 就會出現沒法反序列化的錯誤.app

究其緣由, 是PageImpl沒有無參構造, 其超類Chunk也沒有無參構造; 致使反序列化失敗.dom

解決的方法有兩種, 一是自定義反序列化, 比較麻煩.
另外一種辦法就是自定義Page, 放棄Spring自帶的PageImpl, 這就解決了反序列化的問題. 筆者使用的是後一種方法.
這是筆者自定義的Page實現, 與Spring無關, 能夠在任何項目中通用:ide

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-2363789this

相關文章
相關標籤/搜索