5

I'm getting the following error when using an ObjectMapper to de-serialize an object:

JSONMappingException Can not construct instance of org.springframework.data.Page, problem: abstract types can only be instantiated with additional type information.

I am trying to serialize a JSON string into a Spring data object org.springframework.data.Page which represents a page of type T.

The User class is a simple POJO with first and last name. The JSON string I am deserializing is:

{
    "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
}

This causes the exception:

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

Since Page is a Spring object I cannot modify it which I think makes this a bit different from the way I see this question asked elsewhere. Any thoughts?

1
  • 1
    I tried the deserialization, with the original answer I had proposed. But the Page implementation has virtual properties that cannot be deserialized from the input JSON, and will cause the process to fail. Your best bet would be to write a custom bean to deserialize the JSON into, then instantiate a PageImpl with the values from this custom bean. Commented Jan 21, 2013 at 19:52

2 Answers 2

12

I ended up using something like this, creating a bean as @Perception suggested:

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

and then modify your code to use the concrete class and get the PageImpl:

@SuppressWarnings("unchecked")
Page<User> userPage = ((PageImplBean<User>)new ObjectMapper().readValue(json, new TypeReference<PageImplBean<User>>() {})).pageImpl();
Sign up to request clarification or add additional context in comments.

1 Comment

Nice answer! The only thing that is missing is that the two properties: lastPage and firstPage should be renamed to last and first respectively.
2

You can do this:

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;

    }

}    

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.