java链式编程对象序列化问题

在使用 openfeign 进行服务之间数据传输时发现了一个问题,数据在下游正常处理,响应到调用端数据也读取到了,但是就是 jackson 转对象序列化时出了问题,异常如下

Failed to complete request: feign.codec.DecodeException: Type definition error: [simple type, class com.xxx.common.core.model.PaginationInfo]; nested exception is com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of `com.xxx.common.core.model.PaginationInfo` (no Creators, like default constructor, exist): cannot deserialize from Object value (no delegate- or property-based Creator)

原代码如下

import java.util.List;

public class PaginationInfo {

    private Integer currentPages;
    private Integer total;
    private List records;

    private PaginationInfo(Builder builder) {
        setCurrentPages(builder.currentPages);
        setTotal(builder.total);
        setRecords(builder.records);
    }

    public static Builder builder() {
        return new Builder();
    }

    public Integer getCurrentPages() {
        return currentPages;
    }

    public void setCurrentPages(Integer currentPages) {
        this.currentPages = currentPages;
    }

    public Integer getTotal() {
        return total;
    }

    public void setTotal(Integer total) {
        this.total = total;
    }

    public List getRecords() {
        return records;
    }

    public void setRecords(List records) {
        this.records = records;
    }


    public static final class Builder {
        private Integer currentPages;
        private Integer total;
        private List records;

        private Builder() {
        }

        public Builder currentPages(Integer val) {
            currentPages = val;
            return this;
        }

        public Builder total(Integer val) {
            total = val;
            return this;
        }

        public Builder records(List val) {
            records = val;
            return this;
        }

        public PaginationInfo build() {
            return new PaginationInfo(this);
        }
    }
}

使用了链式编程进行赋值操作,按照上面的提示就是没有默认构造器,将private的构造器修改为public,添加一个默认构造器,效果如下

import java.util.List;

public class PaginationInfo {

    private Integer currentPages;
    private Integer total;
    private List records;

    public PaginationInfo() {
    }

    public PaginationInfo(Builder builder) {
        setCurrentPages(builder.currentPages);
        setTotal(builder.total);
        setRecords(builder.records);
    }

    public static Builder builder() {
        return new Builder();
    }

    public Integer getCurrentPages() {
        return currentPages;
    }

    public void setCurrentPages(Integer currentPages) {
        this.currentPages = currentPages;
    }

    public Integer getTotal() {
        return total;
    }

    public void setTotal(Integer total) {
        this.total = total;
    }

    public List getRecords() {
        return records;
    }

    public void setRecords(List records) {
        this.records = records;
    }


    public static final class Builder {
        private Integer currentPages;
        private Integer total;
        private List records;

        private Builder() {
        }

        public Builder currentPages(Integer val) {
            currentPages = val;
            return this;
        }

        public Builder total(Integer val) {
            total = val;
            return this;
        }

        public Builder records(List val) {
            records = val;
            return this;
        }

        public PaginationInfo build() {
            return new PaginationInfo(this);
        }
    }
}

试了一下,运行正常。

你可能感兴趣的:(java,分布式,java,分布式)