Spring Data Rest 2.1 中的子资源分页 [英] Paginate sub-resources in Spring Data Rest 2.1

查看:27
本文介绍了Spring Data Rest 2.1 中的子资源分页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用默认配置的 Spring Data Rest 2.1.1 Release.考虑以下资源:

I use Spring Data Rest 2.1.1 Release with the default configuration. Considering the following resource:

GET /communities/MyCommunity

{
    "creationDate": "2014-07-16T06:22:37.153+0000",
    "name": "GroupeSEB",
    "_links": {
        "self": {
            "href": "http://localhost:8080/api/communities/GroupeSEB"
        },
        "posts": {
            "href": "http://localhost:8080/api/communities/GroupeSEB/posts"
        }
    }
}

当我获得帖子"子资源时:

When I get the "posts" sub-resource :

GET /communities/MyCommunity/posts

{
    "_embedded": {
        "posts": [
            {
                "creationDate": "2014-07-09T13:09:14.535+0000",
                "id": "53bd3efae4b012818368c549",
                "_links": {
                    "self": {
                        "href": "http://localhost:8080/api/posts/53bd3efae4b012818368c549"
                    } 
                }
            }
        ]
    }
}

未启用分页.由于我的父资源可以聚合大量帖子(其子资源),我如何为每个子资源启用分页?

No pagination is enabled. Since My parent resource can aggregate a large amount of posts (its sub-resource), how can I enable pagination for every sub-resources ?

推荐答案

答案很简单:你不能.理由如下:

The answer is pretty simple: you can't. Here's the rationale:

关联资源表示一个主要实体与一个或多个其他实体之间的关联.因此,为了呈现这些资源,我们查找主要实体并仅访问该属性.这意味着,没有存储库使用,也没有地方分页可以应用,因为整个机制与存储无关.我们在实体实例上操作关联加载的机制是高度存储特定的.

Association resource represent the association between a a primary entity and one or many others. So to render these resources we lookup the primary entity and just access the property. This means, there's no repository usage and no place pagination can be applied as the entire mechanism is store agnostic. We operate on the entity instance The mechanism how associations are loaded is highly store specific.

因此,如果您的域模型中已经有对象关联,那么您就完全受制于商店处理关联的方式.因此,即使您应用了分页,您也必须首先读取所有相关对象以获得它们的 id.

So if you have object associations in your domain model already you're totally bound to the way the store treats associations. So even if you applied pagination, you would have to read all of the related objects in the first place to obtain their ids.

作为一种变通方法,您可以只使用 id 并在该路径上手动公开资源,该路径将使用 id 和关联实体存储库上的专用查询方法.

As a work around you could fall back to ids only and manually expose a resource right at that path which will use the ids and a dedicated query method on the repository of the associated entity.

这篇关于Spring Data Rest 2.1 中的子资源分页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆