Skip to content

序列化 PageHelper 的 Page 问题 #1624

@refeccd

Description

@refeccd

前提

项目使用了 PageHelper 进行查询分页,并提供了一个便捷转换分页模型的方法

 /**
 * PageHelper 返回结果的 Page 转换成 PageResult
 */
public static <T> PageResult<T> transformPageData(Page<T> page) {
    return new PageResult<>(page.getResult(), page.getPageSize(), page.getPageNum(), page.getPages(), (int) page.getTotal());
}

问题

由于服务提供者使用了 PageHelper 的 Page 分页对象,并且 Page 是继承于 ArrayList 的Page#getResult 方法返回的 this
在序列化时,实际写出去的并不是基于接口定义的类型,而是返回的实际类型

JSONB dump
{
    "@type":"com.xxx.xxx.BaseResult#0",
    "@value":{
        "code#1":0,
        "data#2":{
            "@type":"com.xxx.xxx.PageResult#3",
            "@value":{
                "list#4":{
                    "@type":"com.github.pagehelper.Page#5",
                    "@value":[
                        {
                            "@type":"com.xxx.xxx.AmAdminListVo#6",
                            "@value":{
                                "createTime#7":111,
                                "email#8":"xxx.com",
                                "loginName#9":"xxx",
                                "mobile#10":"xxx",
                                "state#11":1,
                                "userId#12":111,
                                "userName#13":"xxx"
                            }
                        }
                    ]
                },
                "pageIndex#15":1,
                "pageSize#16":20,
                "totalCount#17":21,
                "totalPageCount#18":2
            }
        },
        "message#19":"操作成功!",
        "requestId#20":"64A5756161FE3C3DA953CAF2",
        "success#21":true
    }
}

其他信息

public class BaseResult<T> implements Serializable {
    // 省略 get、set
    /**
     * 是否成功
     */
    private boolean success;
    /**
     * 错误码
     */
    private Integer code;
    /**
     * 错误信息
     */
    private String message;
    /**
     * 数据data
     */
    private T data;
    /**
     * requestId
     */
    private String requestId;
}
public class PageResult<T> implements Serializable {
    // 省略 get、set
    /**
     * 总页数
     */
    private int totalPageCount = 1;
    /**
     * 总记录数
     */
    private int totalCount = 0;
    /**
     * 每页行数
     */
    private int pageSize = 15;
    /**
     * 当前页码,-1时不查询分页
     */
    private int pageIndex = 1;
    /**
     * 列表数据
     */
    private List<T> list;
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingfixedquestionFurther information is requested

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions