Spring Data Rest 2.1中的Paginate子资源

Max*_*ime 5 spring pagination spring-data-rest

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

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"
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

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

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"
                    } 
                }
            }
        ]
    }
}
Run Code Online (Sandbox Code Playgroud)

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

Oli*_*ohm 3

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

关联资源表示主要实体与一个或多个其他实体之间的关联。因此,为了呈现这些资源,我们查找主要实体并访问该属性。这意味着,没有存储库使用,也不能应用位置分页,因为整个机制与存储无关。我们对实体实例进行操作 关联的加载机制是高度特定于存储的。

因此,如果您的域模型中已经存在对象关联,那么您就完全受到商店处理关联的方式的约束。因此,即使您应用了分页,您也必须首先读取所有相关对象才能获取它们的 id。

作为解决方法,您可以仅使用 ids 并手动公开该路径上的资源,该资源将使用 ids 和关联实体存储库上的专用查询方法。