使用Spring我们如何从Object返回分页的对象列表

时间:2019-02-28 09:21:32

标签: spring-boot pagination spring-mongodb

我接受来自服务的分页响应并对其执行操作。导致一个奇异的对象。 包含来自mongo的聚合queryResult的对象, 如何从端点返回单个对象内对象的分页响应。

{
"fullName:"abc",

"educationData":[

// paginated response from another service.
//queryResult

],

"cellNo" : "12345",
"address" : "pqr";
}

我想对“ educationData”的列表进行分页, 但是我最终对整个对象进行了分页。

1 个答案:

答案 0 :(得分:1)

由于您已经收到来自服务的分页响应,因此您可以转发相同的响应。 创建一个实现PageImpl的类,该类将为您提供所有分页信息,例如:-

public class CustomPageImpl extends PageImpl<T> {

  @JsonCreator
  @JsonIgnoreProperties(ignoreUnknown = true)
  public CustomPageImpl(@JsonProperty("content") List<T> content,
      @JsonProperty("number") int page,
      @JsonProperty("size") int size,
      @JsonProperty("totalElements") long totalElements) {
    super(content, new PageRequest(page, size), totalElements);
  }
}

更改您的响应对象,并在CustomPageImpl类中附加这些字段。

{
"fullName:"abc",

"educationData":[

// paginated response from another service.
//queryResult

],

"cellNo" : "12345",
"address" : "pqr",
"totalElements": 0,
"totalPages": 0
}