如何在spring-data-rest中禁用JpaRepository的分页 [英] How to disable paging for JpaRepository in spring-data-rest

查看:152
本文介绍了如何在spring-data-rest中禁用JpaRepository的分页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用来创建Rest-Endpoints.默认情况下,所有JpaRepository都启用分页,这是一件好事.但是我有一个遗留应用程序,我们将其移植到不支持分页的新堆栈中.我想根据URL参数禁用分页,以便仍然能够在新的应用程序代码中使用分页.

I'm using spring-data-rest with JpaRepository to create the Rest-Endpoints. By default, paging is enabled for all JpaRepository, what is a good thing. But I have a legacy application that we port to our new stack that does not support paging. I would like to disable paging depending on an URL-Parameter to still be able to use paging in new application code.

我尝试了各种方法来显示有无页面调度的资源:

I tried various approaches to expose the resources with and without paging:

  • 使用CrudRepository:导致仅具有未分页的端点,并且缺少方法flush.
  • 在我的存储库界面中覆盖List<T> findAll()方法,并用RestResource对其进行注释.我希望该方法将作为搜索方法公开,但事实并非如此.
  • 使用@RestResource(exported=false)注释Page<T> findAll(Pageable pageable)并注释List<T> findAll(),如之前的项目符号所示.我希望这可以代替默认方法.但这毕竟不是有效的解决方案,因为仅公开了未分页的端点.
  • 通过size=-1获得无限结果->使用默认的分页大小
  • Use CrudRepository: Results in only having an unpaged endpoint and the method flush is missing.
  • Override the List<T> findAll() method in my repository interface and annotated it with RestResource. I would have expected that the method will be exposed as search method, but it is not.
  • Annotate Page<T> findAll(Pageable pageable) with @RestResource(exported=false) and annotate List<T> findAll() as in the bullet before. I have hopped that this replaces the default method. But this is not valid solution anyway, because only a non paged endpoint is exposed.
  • Pass size=-1 to get an unlimited result -> Default paging size is used

我已经看到弹簧控制器RepositoryEntityController使用RepositoryInvoker来调用存储库中的方法. Pageable使用PageableHandlerMethodArgumentResolver解析,该PageableHandlerMethodArgumentResolver始终返回可分页(在查询中指定,带注释或默认的可分页). 我目前看到的唯一解决方案是实现自定义PageableHandlerMethodArgumentResolver,如果传递了自定义url参数,则该返回空值.

I've seen that the spring-controller RepositoryEntityController uses a RepositoryInvoker to call the methods on the repository. The Pageable is resolved using the PageableHandlerMethodArgumentResolver which always returns a pageable (specified in query, annotated or default pageable). The only solution that I see for the moment is to implement a custom PageableHandlerMethodArgumentResolver that returns null, if a custom url parameter is passed.

您还知道其他解决方案吗?或者将来有类似的计划吗?

Do you know any other solutions or is anything similar planned for the future?

推荐答案

我使用PagingAndSortingRepository和此配置来设置我的pageableResolver:

I use PagingAndSortingRepository and this config to set my pageableResolver:

@Configuration
public class RestApiConfiguration extends RepositoryRestConfigurerAdapter {

    @Bean
    public HateoasPageableHandlerMethodArgumentResolver customResolver(
        HateoasPageableHandlerMethodArgumentResolver pageableResolver) {
        pageableResolver.setOneIndexedParameters(true);
        pageableResolver.setFallbackPageable(new PageRequest(0, Integer.MAX_VALUE));
        pageableResolver.setMaxPageSize(Integer.MAX_VALUE);
        return pageableResolver;
    }
}

请参阅: https://jira.spring.io/browse/DATACMNS-929

这样,如果请求中包含页面和大小,则您将获得请求的页面,但是如果请求中未包含页面和大小,则将获得所有记录.在这两种情况下,如果都指定了排序,则将其用于对数据进行排序. 在第二种情况下,记录将在页面内返回,但是我可以接受.

This way if page and size are included in the request you get the page requested, but if they are not in the request you get all records. In both cases if a sort is specified it is used to sort the data. In the second case, the records are returned inside a page, but I can live with that.

编辑 https://jira.spring.io/browse/DATACMNS-929 已修复,因此,使用新版本,您将能够使用null fallbackPageable配置解析器.这样,当存在可分页数据(即pagesize)时,您将检索一页,但当不存在时,您将检索所有记录:

EDIT https://jira.spring.io/browse/DATACMNS-929 has been fixed, so with the new versions you'll be able to configure your resolver with a null fallbackPageable. That way, when pageable data (ie page and size) is present you retrieve one page, but when it's not you retrieve all records:

@Configuration
public class RestApiConfiguration extends RepositoryRestConfigurerAdapter {

    @Bean
    public HateoasPageableHandlerMethodArgumentResolver customResolver(
        HateoasPageableHandlerMethodArgumentResolver pageableResolver) {
        pageableResolver.setOneIndexedParameters(true);
        pageableResolver.setFallbackPageable(null);
        return pageableResolver;
    }
}

这篇关于如何在spring-data-rest中禁用JpaRepository的分页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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