Spring RestTemplate with paginated API

前端 未结 7 2088
被撕碎了的回忆
被撕碎了的回忆 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:47

    When migrating from Spring Boot 1.x to 2.0, changed the code reading the Rest API response as

    import com.fasterxml.jackson.annotation.JsonCreator;
    import com.fasterxml.jackson.annotation.JsonProperty;
    import com.fasterxml.jackson.databind.JsonNode;
    
    import org.springframework.data.domain.PageImpl;
    import org.springframework.data.domain.PageRequest;
    import org.springframework.data.domain.Pageable;
    
    import java.util.ArrayList;
    import java.util.List;
    
    public class RestPageImpl extends PageImpl{
    
        @JsonCreator(mode = JsonCreator.Mode.PROPERTIES)
        public RestPageImpl(@JsonProperty("content") List content,
                            @JsonProperty("number") int number,
                            @JsonProperty("size") int size,
                            @JsonProperty("totalElements") Long totalElements,
                            @JsonProperty("pageable") JsonNode pageable,
                            @JsonProperty("last") boolean last,
                            @JsonProperty("totalPages") int totalPages,
                            @JsonProperty("sort") JsonNode sort,
                            @JsonProperty("first") boolean first,
                            @JsonProperty("numberOfElements") int numberOfElements) {
    
            super(content, PageRequest.of(number, size), totalElements);
        }
    
        public RestPageImpl(List content, Pageable pageable, long total) {
            super(content, pageable, total);
        }
    
        public RestPageImpl(List content) {
            super(content);
        }
    
        public RestPageImpl() {
            super(new ArrayList<>());
        }
    }
    

提交回复
热议问题