Micha Reiser
Micha Reiser

Reputation: 201

How to disable paging for JpaRepository in spring-data-rest

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:

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?

Upvotes: 12

Views: 14653

Answers (5)

azizairo
azizairo

Reputation: 11

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.

Upvotes: 1

Zack
Zack

Reputation: 4037

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

Upvotes: 2

Ran
Ran

Reputation: 502

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

Upvotes: 3

Triqui
Triqui

Reputation: 281

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

Upvotes: 11

Marc Zampetti
Marc Zampetti

Reputation: 733

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.

Upvotes: 4

Related Questions