Spring RestTemplate with paginated API

前端 未结 7 2102
被撕碎了的回忆
被撕碎了的回忆 2020-11-27 17:22

Our REST APIs are returning results in Pages. Here is an example of one Controller

@RequestMapping(value = \"/search\", method = RequestMethod.GET, produces          


        
7条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-11-27 17:29

    Changed the code reading the Rest API response as;

    ParameterizedTypeReference> responseType = new ParameterizedTypeReference>() { };
    
    ResponseEntity> result = restTemplate.exchange(url, HttpMethod.GET, null/*httpEntity*/, responseType);
    
    List searchResult = result.getBody().getContent();
    

    And here is the class I created for RestResponsePage

    package com.basf.gb.cube.seq.vaadinui.util;
    
    import java.util.ArrayList;
    import java.util.List;
    
    import org.springframework.data.domain.PageImpl;
    import org.springframework.data.domain.Pageable;
    
    public class RestResponsePage extends PageImpl{
    
      private static final long serialVersionUID = 3248189030448292002L;
    
      public RestResponsePage(List content, Pageable pageable, long total) {
        super(content, pageable, total);
        // TODO Auto-generated constructor stub
      }
    
      public RestResponsePage(List content) {
        super(content);
        // TODO Auto-generated constructor stub
      }
    
      /* PageImpl does not have an empty constructor and this was causing an issue for RestTemplate to cast the Rest API response
       * back to Page.
       */
      public RestResponsePage() {
        super(new ArrayList());
      }
    
    } 
    

提交回复
热议问题