4

使用ObjectMapper反序列化对象时出现以下错误:

JSONMappingException 无法构造 org.springframework.data.Page 的实例,问题:抽象类型只能通过附加类型信息进行实例化。

我正在尝试将 JSON 字符串序列化为org.springframework.data.Page代表页面类型的 Spring 数据对象T

该类User是一个简单的 POJO,带有firstlast名称。我反序列化的 JSON 字符串是:

{
    "content": [
        {
            "firstname": "John",
            "lastname": "Doe"
        },
        {
            "firstname": "Jane",
            "lastname": "Doe"
        }
    ],
    "size": 2,
    "number": 0,
    "sort": [
        {
            "direction": "DESC",
            "property": "timestamp",
            "ascending": false
        }
    ],
    "totalPages": 150,
    "numberOfElements": 100,
    "totalElements": 15000,
    "firstPage": true,
    "lastPage": false
}

这会导致异常:

Page<User> userPage = (Page<User>) new ObjectMapper().mapToJavaObject(json, new TypeReference<Page<User>>(){};

由于Page是 Spring 对象,我无法修改它,我认为这与我在其他地方看到的这个问题的方式有点不同。有什么想法吗?

4

2 回答 2

11

我最终使用了类似的东西,按照@Perception 的建议创建了一个bean:

import java.util.ArrayList;
import java.util.List;

import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;

public class PageImplBean<T> extends PageImpl<T> {

    private static final long serialVersionUID = 1L;
    private int number;
    private int size;
    private int totalPages;
    private int numberOfElements;
    private long totalElements;
    private boolean previousPage;
    private boolean firstPage;
    private boolean nextPage;
    private boolean lastPage;
    private List<T> content;
    private Sort sort;

    public PageImplBean() {
        super(new ArrayList<T>());
    }

    public int getNumber() {
        return number;
    }

    public void setNumber(int number) {
        this.number = number;
    }

    public int getSize() {
        return size;
    }

    public void setSize(int size) {
        this.size = size;
    }

    public int getTotalPages() {
        return totalPages;
    }

    public void setTotalPages(int totalPages) {
        this.totalPages = totalPages;
    }

    public int getNumberOfElements() {
        return numberOfElements;
    }

    public void setNumberOfElements(int numberOfElements) {
        this.numberOfElements = numberOfElements;
    }

    public long getTotalElements() {
        return totalElements;
    }

    public void setTotalElements(long totalElements) {
        this.totalElements = totalElements;
    }

    public boolean isPreviousPage() {
        return previousPage;
    }

    public void setPreviousPage(boolean previousPage) {
        this.previousPage = previousPage;
    }

    public boolean isFirstPage() {
        return firstPage;
    }

    public void setFirstPage(boolean firstPage) {
        this.firstPage = firstPage;
    }

    public boolean isNextPage() {
        return nextPage;
    }

    public void setNextPage(boolean nextPage) {
        this.nextPage = nextPage;
    }

    public boolean isLastPage() {
        return lastPage;
    }

    public void setLastPage(boolean lastPage) {
        this.lastPage = lastPage;
    }

    public List<T> getContent() {
        return content;
    }

    public void setContent(List<T> content) {
        this.content = content;
    }

    public Sort getSort() {
        return sort;
    }

    public void setSort(Sort sort) {
        this.sort = sort;
    }

    public PageImpl<T> pageImpl() {
        return new PageImpl<T>(getContent(), new PageRequest(getNumber(),
                getSize(), getSort()), getTotalElements());
    }
}

然后修改您的代码以使用具体类并获取 PageImpl:

@SuppressWarnings("unchecked")
Page<User> userPage = ((PageImplBean<User>)new ObjectMapper().readValue(json, new TypeReference<PageImplBean<User>>() {})).pageImpl();
于 2013-09-17T10:15:39.963 回答
2

你可以这样做:

public class YourClass {

    static class CustomPage extends PageImpl<User> {

        @JsonCreator(mode = Mode.PROPERTIES)
        public CustomPage(@JsonProperty("content") List<User> content, @JsonProperty("number") int page, @JsonProperty("size") int size, @JsonProperty("totalElements") long total) {
            super(content, new PageRequest(page, size), total);
        }

    }

    public Page<User> makeRequest(String json) {

        Page<User> pg = new ObjectMapper().readValue(json, CustomPage.class);
        return pg;

    }

}    
于 2016-10-20T18:56:02.927 回答