嵌入式资源分页用例 [英] Use case for Pagination of Embedded resources

查看:25
本文介绍了嵌入式资源分页用例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个用例,我正在为 SDR 苦苦挣扎,如下所示 -

There is a use case I am struggling with SDR as below -

有用户表和 RefSecQuestion 表

THere is User Table and RefSecQuestion tables

User -> ManyTOOne -> RefSecQuestion , RefSecQuestion -> OneToMany -> User

有 User Table 和 UserFriends 表

THere is User Table and UserFriends tables

User -> OneToMany UserFriends , UserFriends -> ManyToOne -> User

有一个要求,当我去/users/{id}/userFriends 时,然后默认显示 UserProjection 中的 firstname 、 lastname 等

There is a requirement that when I go /users/{id}/userFriends , then firstname , lastname etc from UserProjection should be shown by default

因此,我在 UserRepository 中启用了 excerptProjection 并且它工作正常.我预计这里会有大约 100 个结果,所以如果这个结果没有分页就可以了.

As a result, I enabled excerptProjection in UserRepository and it works fine. I expect about 100 results here so that is fine if this result is not paginated.

但是,现在因为 RefSecQuestion 也与 User 相关,所以当我去的时候会发生什么/refSecQuestions -> 此页面挂起,因为它试图用 UserProjection 替换用户链接.对于大多数用户来说,RefSecQuestion 表中存在一个问题,因此分页丢失导致分页.

But , now since RefSecQuestion is also related to User , what happens is that when I go /refSecQuestions -> this page hangs since it is trying to substitute user link with UserProjection. The RefSecQuestion table is skewed with one question for most of users and therefore the page breaks due to loss of pagination.

因为我不能在这里选择单向性,因为需要两个 url,即

since i cant choose unidirectionality here as both url are needed i.e

/users/{id}/userFriends
/refSecQuestions/users  

我找到的最接近的答案是选择单向性,即我在 RefSEcQuestion 中将用户的 Rest Export 设置为 false

THe closest answer I found was to choose unidirectinality which is that I set Rest Export to false for User in RefSEcQuestion

推荐答案

我终于能够得到我想要的结果并在这里为所有 SDR 用户发帖.我想在这个 URL 中进行分页 - 其中 User -> One To Many -> UserLanguages

Finally I am able to get my desired results and am posting here for all SDR users. I wanted to have pagination in this URL - where User -> One To Many -> UserLanguages

/users/{id}/userLanguages

现在使用默认的 SDR 配置,嵌入式资源不会被分页,因此我必须手动公开它们,解决方法如下所示,它仍然需要非常少的代码行 -

Now Using default SDR configuration, embedded resources don't get paginated and therefore I have to manually expose them and the Workaround is as below which still takes very less lines of code -

@RestController
public class MainController {

    @RequestMapping(value = "/users/{id}/userLanguages", method = RequestMethod.GET)
    @PreAuthorize("permitAll")
    public ModelAndView findUserLanguages(@PathVariable Integer id) {
        ModelAndView model = new ModelAndView("forward:/userLanguages/search/findByUserId?userId=" + id);
        return model;
    }

然后,在 UserLangRepository

then, in UserLangRepository

public interface UserLanguageRepository extends BaseRepository<UserLanguage, Integer> {

    Page<UserLanguage> findByUserId(@Param("userId") Integer userId, Pageable pageable);
}

这里的名称 findByUserId 遵循 Spring Data Query Derivation 规则,其中 UserLanguage 中有 user 列,User 中有 id 列.然后对以下 URL 进行分页并有其他选项,例如排序、大小等.

Here name findByUserId follows Spring Data Query Derivation rule where there is user column in UserLanguage and id column in User. Then following URL is paginated and have other options as well like sort, size etc..

http://localhost:8585/MYAPP/users/3/userLanguages

但是存在下一个和上一个链接指向转发链接的问题..

There is an issue however that next and prev links point to forwarded link..

这篇关于嵌入式资源分页用例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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