Spring Data Rest中嵌套资源的分页 [英] Pagination in Spring Data Rest for nested resources

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

问题描述

当访问下面的 URL 时,我得到分页响应

When the below URL is visited, I get paginations in response

/api/userPosts/

{
  "_links" : {
    "self" : {
      "href" : "/api/userPosts{?page,size,sort}",
      "templated" : true
    },
    "next" : {
      "href" : api/userPosts?page=1&size=20{&sort}",
      "templated" : true
    }
  },
  "_embedded" : {
    "userPosts" : [ {
     ...

但是,当访问以下 URL 时,Spring Data REST 没有开箱即用的分页 -

However when visiting following URL, there is no pagination out of box by Spring Data REST -

/api/users/4/userPosts

{
  "_embedded" : {
    "userPosts" : [ {

UserRepository 和 UserPostRepository 都是带分页的 JPARepository.结果,第二个 URL 抛出 GC Overhead exceeded 错误,因为返回结果的行数很大.

Both UserRepository and UserPostRepository are JPARepository with pagination. As a result, the second URL is throwing GC overhead exceeded error since the row count for results returned is huge.

@RepositoryRestResource(excerptProjection = UserProjection.class)
public interface UserRepository extends BaseRepository<User, Integer>, UserRepositoryCustom {

}

public interface UserPostRepository extends BaseRepository<UserPost, Long> {

}


@NoRepositoryBean
public interface BaseRepository<T, N extends Serializable> extends JpaRepository<T, N>, QueryDslPredicateExecutor<T> {

}

还有什么方法可以使用第二个 URL 进行分页?

Any way to have pagination with second URL as well ?

推荐答案

痛苦的简短回答:不.绝对不是.

Short painful answer: No. Absolutely not.

长更痛苦的回答:是的.通过重写大部分 Spring Data、JPA 和 Hibernate.问题的核心是,当您请求嵌套实体(集合与否)时,嵌套实体不是来自存储库的查询.但是是从实体返回的.Spring Data/JPA 中没有用于分页的机制

Long even more painful answer: Yes. By rewriting large sections of Spring Data, JPA and Hibernate. The core of the problem is that when you are requesting the the nested entity (collection or not) that nested entity is NOT queries from repository. But is is returned from the entity. There are no mechanics in Spring Data / JPA for paging

Spring REST 中的/api/users/4/userPosts 请求基本上是这样的:

What /api/users/4/userPosts request in Spring REST does is basicly this:

User user = userRepository.findOne(4);
return user.userPosts;

所以检索 user.userPosts 是对嵌套实体的 Eager 或 Lazy 引用,并且无法对其进行分页.

So retrieving user.userPosts is Eager or Lazy reference to an nested entity and there is not way to page that.

实现这一目标的最简单也是唯一的解决方案是:1.创建Spring Data搜索查询:UserPostRepository.findByUserId(Long id, Pagination pa)2.创建自定义Spring MVC控制器进行映射

Easiest and only solution to achieve this is : 1. create Spring Data search query: UserPostRepository.findByUserId(Long id, Pagination pa) 2. Create custom Spring MVC controller for mapping

   @Get("/api/users/{id}/userPosts")
   public Page<UserPost> getUserPostsByUserId(Long id, Pagination pagi) {
     return userPostRepository.findByUserId(id, pagi);

  1. 重要!你不能!将 user.userPosts 注释为嵌套在 User 实体中,否则请求映射将发生冲突.
  2. 如果您想要用户实体 JSON 中此路径的导航超链接,那么您需要用户实体 JSON 创建的自定义处理.它的文档记录很差,并且没有任何示例涵盖您需要探索一下的确切用例.

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

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