【问题标题】:Using spring how do we return paginated list of objects from an Object使用spring我们如何从一个对象返回分页的对象列表
【发布时间】:2019-02-28 09:21:32
【问题描述】:

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

{
"fullName:"abc",

"educationData":[

// paginated response from another service.
//queryResult

],

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

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

【问题讨论】:

标签: spring-boot pagination spring-mongodb


【解决方案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
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-12-14
    • 1970-01-01
    • 2020-05-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-01
    相关资源
    最近更新 更多