How to consume Page response using Spring RestTemplate

前端 未结 7 876
感动是毒
感动是毒 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:39

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

    The code should look like this:

    ResponseEntity> response = restTemplate.exchange(getLocalhost("/story"),
            HttpMethod.GET, null, new ParameterizedTypeReference>() {});
    PagedResources storiesResources = response.getBody();
    Collection 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

提交回复
热议问题