Spring RestTemplate with paginated API

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

    I had to make a small change so it would ignore the unknown property of empty that seems to have been recently introduced.

    import com.fasterxml.jackson.annotation.JsonCreator;
    import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
    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;
    
    @JsonIgnoreProperties(ignoreUnknown = true)
    public class RestResponsePage extends PageImpl {
        @JsonCreator(mode = JsonCreator.Mode.PROPERTIES)
        public RestResponsePage(@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 RestResponsePage(List content, Pageable pageable, long total) {
            super(content, pageable, total);
        }
    
        public RestResponsePage(List content) {
            super(content);
        }
    
        public RestResponsePage() {
            super(new ArrayList<>());
        }
    }
    

提交回复
热议问题