使用 spring 我们如何 return 从对象中分页对象列表

Using spring how do we return paginated list of objects from an Object

我正在接受来自服务的分页响应并对其执行操作。结果是一个单一的对象。 包含来自 mongo 的聚合查询结果的对象, 如何 return 来自端点的单个对象内对象的分页响应。

{
"fullName:"abc",

"educationData":[

// paginated response from another service.
//queryResult

],

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

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

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

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 class 的这些字段。

{
"fullName:"abc",

"educationData":[

// paginated response from another service.
//queryResult

],

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