Spring Data Rest中嵌套资源的分页

2024-08-23Java开发问题
2

本文介绍了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中嵌套资源的分页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

如何使用 JAVA 向 COM PORT 发送数据?
How to send data to COM PORT using JAVA?(如何使用 JAVA 向 COM PORT 发送数据?)...
2024-08-25 Java开发问题
21

如何使报表页面方向更改为“rtl"?
How to make a report page direction to change to quot;rtlquot;?(如何使报表页面方向更改为“rtl?)...
2024-08-25 Java开发问题
19

在 Eclipse 项目中使用西里尔文 .properties 文件
Use cyrillic .properties file in eclipse project(在 Eclipse 项目中使用西里尔文 .properties 文件)...
2024-08-25 Java开发问题
18

有没有办法在 Java 中检测 RTL 语言?
Is there any way to detect an RTL language in Java?(有没有办法在 Java 中检测 RTL 语言?)...
2024-08-25 Java开发问题
11

如何在 Java 中从 DB 加载资源包消息?
How to load resource bundle messages from DB in Java?(如何在 Java 中从 DB 加载资源包消息?)...
2024-08-25 Java开发问题
13

如何更改 Java 中的默认语言环境设置以使其保持一致?
How do I change the default locale settings in Java to make them consistent?(如何更改 Java 中的默认语言环境设置以使其保持一致?)...
2024-08-25 Java开发问题
13