且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在spring-data-rest中禁用JpaRepository的分页

更新时间:2022-06-18 00:44:05

我使用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;
    }
}