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

查看:82
本文介绍了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天全站免登陆