52

I'm using spring data (mongoDb) and I've got my repository:

public interface StoriesRepository extends PagingAndSortingRepository<Story, String> {}

Then i have a controller:

@RequestMapping(method = RequestMethod.GET)
public ResponseEntity<Page<StoryResponse>> getStories(Pageable pageable) {
    Page<StoryResponse> stories = storiesRepository.findAll(pageable).map(StoryResponseMapper::toStoryResponse);
    return ResponseEntity.ok(stories);
}

Everything works fine, but I can't consume my endpoint using RestTemplate getForEntity method:

def entity = restTemplate.getForEntity(getLocalhost("/story"), new TypeReference<Page<StoryResponse>>(){}.class)

What class should I provide to successfully deserialize my Page of entities?

bgalek
  • 553
  • 1
  • 5
  • 7

6 Answers6

60
new TypeReference<Page<StoryResponse>>() {}

The problem with this statement is that Jackson cannot instantiate an abstract type. You should give Jackson the information on how to instantiate Page with a concrete type. But its concrete type, PageImpl, has no default constructor or any @JsonCreators for that matter, so you can not use the following code either:

new TypeReference<PageImpl<StoryResponse>>() {}

Since you can't add the required information to the Page class, It's better to create a custom implementation for Page interface which has a default no-arg constructor, as in this answer. Then use that custom implementation in type reference, like following:

new TypeReference<CustomPageImpl<StoryResponse>>() {}

Here are the custom implementation, copied from linked question:

public class CustomPageImpl<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 CustomPageImpl() {
        super(new ArrayList<>());
    }

    @Override
    public int getNumber() {
        return number;
    }

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

    @Override
    public int getSize() {
        return size;
    }

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

    @Override
    public int getTotalPages() {
        return totalPages;
    }

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

    @Override
    public int getNumberOfElements() {
        return numberOfElements;
    }

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

    @Override
    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;
    }

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

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

    @Override
    public Sort getSort() {
        return sort;
    }

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

    public Page<T> pageImpl() {
        return new PageImpl<>(getContent(), new PageRequest(getNumber(),
                getSize(), getSort()), getTotalElements());
    }
}
Community
  • 1
  • 1
Ali Dehghani
  • 46,221
  • 15
  • 164
  • 151
  • 3
    Don't forget to add `@JsonIgnoreProperties(ignoreUnknown = true)` as a class annotation. This property will help ignore the additional features that would require custom mapping. – Menelaos Kotsollaris Aug 07 '17 at 17:49
  • 1
    I still gives me the error. Please look at https://stackoverflow.com/questions/50826061/jsonmappingexception-when-testing-endpoints-with-pageable-field – Shaunyl Jun 13 '18 at 19:39
37

I know this thread is a little old, but hopefully someone will benefit from this.

@Ali Dehghani's answer is good, except that it re-implements what PageImpl<T> has already done. I considered this to be rather needless. I found a better solution by creating a class that extends PageImpl<T> and specifies a @JsonCreator constructor:

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.company.model.HelperModel;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;

import java.util.List;

public class HelperPage extends PageImpl<HelperModel> {

    @JsonCreator
    // Note: I don't need a sort, so I'm not including one here.
    // It shouldn't be too hard to add it in tho.
    public HelperPage(@JsonProperty("content") List<HelperModel> content,
                      @JsonProperty("number") int number,
                      @JsonProperty("size") int size,
                      @JsonProperty("totalElements") Long totalElements) {
        super(content, new PageRequest(number, size), totalElements);
    }
}

Then:

HelperPage page = restTemplate.getForObject(url, HelperPage.class);

This is the same as creating a CustomPageImpl<T> class but allows us to take advantage of all the code that's already in PageImpl<T>.

teuber789
  • 1,527
  • 16
  • 28
  • 2
    I'm getting this exception when i tried to use your approach: `org.springframework.http.converter.HttpMessageConversionException: Type definition error: [simple type, class org.springframework.data.domain.Pageable]; nested exception is com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Can not construct instance of org.springframework.data.domain.Pageable (no Creators, like default construct, exist): abstract types either need to be mapped to concrete types, have custom deserializer, or contain additional type information` – Sri Sep 19 '17 at 03:07
  • @Sriram Did you check that 1. your `HelperPage` constructor has the `@JsonCreator` annotation? 2. You get an `HelpPage` from your restTemplate? – Isammoc Sep 19 '17 at 10:21
  • 1
    this is a better solution than the accepted answer. Adding `@JsonIgnoreProperties(ignoreUnknown = true)` after `@JsonCreator also prevents problems deserialising when there are unknown properties – robjwilkins Feb 28 '18 at 10:25
  • 2
    I still gives me the error. Please look at https://stackoverflow.com/questions/50826061/jsonmappingexception-when-testing-endpoints-with-pageable-field – Shaunyl Jun 13 '18 at 19:39
10

As "pathfinder" mentioned you can use exchange method of RestTemplate. However instead of passing ParameterizedTypeReference<Page<StoryResponse>>() you should pass ParameterizedTypeReference<PagedResources<StoryResponse>>(). When you get the response you could retrieve the content - Collection<StoryResponse>.

The code should look like this:

ResponseEntity<PagedResources<StoryResponse>> response = restTemplate.exchange(getLocalhost("/story"),
        HttpMethod.GET, null, new ParameterizedTypeReference<PagedResources<StoryResponse>>() {});
PagedResources<StoryResponse> storiesResources = response.getBody();
Collection<StoryResponse> stories = storiesResources.getContent();

Apart from the content storiesResources holds page metadata and links too.

A more step-by-step explanation is available here: https://stackoverflow.com/a/46847429/8805916

Vladimir Mitev
  • 431
  • 6
  • 8
7

If you use spring-cloud-openfeign you can use PageJacksonModule. Just register PageJacksonModule in your object mapper:

final ObjectMapper mapper = new ObjectMapper()
mapper.registerModule(new PageJacksonModule());
Egor
  • 2,122
  • 2
  • 21
  • 23
2

If you looking at this thread, and if you try this answer https://stackoverflow.com/a/44895867/8268335

You will meet the 2nd problem:

Can not construct instance of org.springframework.data.domain.Pageable

Then I find the perfect solution from here: https://stackoverflow.com/a/42002709/8268335

I create the class RestPageImpl from the answer above and problem solved.

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
Terrence Wei
  • 195
  • 1
  • 7
-1

You can probably use exchange method of restTemplate and get the body from it..

Check the following answer https://stackoverflow.com/a/31947188/3800576. This might help you

Community
  • 1
  • 1
pathfinder
  • 127
  • 4
  • 18