How to disable paging for JpaRepository in spring-data-rest
Asked Answered
L

5

12

I'm using 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:

  • 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

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?

Langur answered 13/7, 2015 at 9:36 Comment(0)
C
11

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;
    }
}

See: 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.

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;
    }
}
Cecilacecile answered 28/10, 2016 at 19:2 Comment(0)
R
4

You could add your own methods to the Repository interface, and have a return type of List<DomainObject> or Collection<DomainObject> and no Pageable parameter. That will cause a non-paged response to be used. You could then point your legacy client at those methods instead of the default ones.

Or, you could configure the default page size to be very large. Set spring.data.rest.default-page-size in application.properties.

Rub answered 13/7, 2015 at 19:21 Comment(0)
G
3

This is not the cleanest solution, but the following code with allow you to pass unpaged=true as a query parameter, in order to get all the result in one page.

@Configuration
public class RestApiServletConfig implements WebMvcConfigurer {
    @Override
    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> resolvers) {
        PageableHandlerMethodArgumentResolver pageableResolver = new PageableHandlerMethodArgumentResolver() {
            @Override
            public Pageable resolveArgument(MethodParameter methodParameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) {
                if ("true".equals(webRequest.getParameter(getParameterNameToUse("unpaged", methodParameter)))) {
                    return Pageable.unpaged();
                }

                return super.resolveArgument(methodParameter, mavContainer, webRequest, binderFactory);
            }
        };
        resolvers.add(pageableResolver);
    }
}
Gillett answered 16/6, 2019 at 8:53 Comment(0)
D
2

The highest rated answer indicates a null for setFallbackPageable, which is no longer valid. Use the default static constructor for PageRequest like this:

@Configuration
public class RestApiConfiguration extends RepositoryRestConfigurerAdapter {

    @Bean
    public HateoasPageableHandlerMethodArgumentResolver customResolver(
        HateoasPageableHandlerMethodArgumentResolver pageableResolver) {
        pageableResolver.setOneIndexedParameters(true);
        pageableResolver.setFallbackPageable(PageRequest.of(0, Integer.MAX_VALUE));
        pageableResolver.setMaxPageSize(Integer.MAX_VALUE);
        return pageableResolver;
    }
}
Dye answered 18/11, 2019 at 3:27 Comment(0)
L
1

You can achive it like this:

@Configuration
@RequiredArgsConstructor
public class PageableHandlerConfiguration {

    private final PageableHandlerMethodArgumentResolver resolver;

    @PostConstruct
    private void configurePageableResolver() {

        resolver.setFallbackPageable(Pageable.unpaged());
    }

}

Now, if you did not pass pageable parameters in request you will get unpaged result.

Liebermann answered 24/4 at 9:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.