How to consume Page response using Spring RestTemplate

前端 未结 7 872
感动是毒
感动是毒 2020-12-02 16:42

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

public interface StoriesRepository extends PagingAndSortingRepository {}
<         


        
相关标签:
7条回答
  • 2020-12-02 17:27
    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());
        }
    }
    
    0 讨论(0)
  • 2020-12-02 17:31

    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());
    
    0 讨论(0)
  • 2020-12-02 17:35

    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>.

    0 讨论(0)
  • 2020-12-02 17:35

    I can only make it work downgrading Spring library to 1.* and not using 2.* I had to create my own code for the Page which does not extends PageImpl

    0 讨论(0)
  • 2020-12-02 17:39

    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

    0 讨论(0)
  • 2020-12-02 17:41

    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

    0 讨论(0)
提交回复
热议问题