谷歌应用引擎使用nextPageToken一次查询50个结果



我的GAE云端点中有以下Api方法:

@ApiMethod(name = "getConferences", path = "get_conferences", httpMethod = ApiMethod.HttpMethod.POST)
    public List<Conference> getConferences(@Named("userId") Long userId) {
        List<Conference> conferenceList = ofy().load().type(Conference.class)
                    .ancestor(Key.create(User.class, userId))
                    .order("-createdDate").list();

        return  conferenceList; 

}

它工作得很好,并为我返回了给定用户创建的按日期排序的所有会议。Conference类具有以下属性以指定它具有User父级:

@Parent
private Key<User> userKey;

我的问题是,我如何改变上面的方法,一次只返回50个结果(会议),并且能够指定一个参数,该参数需要像nextPageToken这样的东西来给我接下来的50个结果?

我已经在其他API方法中看到了这一点,但似乎找不到一个有效的GAE或Cloud端点的好例子。

  1. 与其返回List<Conference>,不如返回com.google.api.server.spi.response.CollectionResponse<Conference>
  2. 添加命名参数,例如@Named("nextPageToken") String pageToken
  3. 如果是pageToken != null,则在.order()之后需要链接.startAt(Cursor.fromWebSafeString(pageToken))
  4. 您还需要添加.limit(50),因为您希望它是您的页面大小
  5. 使用具有getStartCursor()方法的.iterator()代替.list()。使用this和迭代器来构造CollectionResponse

有关如何使用游标的非端点示例,请参阅本页。剩下的应该是琐碎的。

最新更新