44

I have a PagingandSorting Repository which has a method that accecpts a pageable object. I also have a controller that accepts a pageable object through the URL.

My use case is that, if a user specifies a page size parameter in the URL i must take that value for the pageable object. If he does not mention take a default value of 50.

But the pageable object defaults to 20 right now.

Any Suggestions would help

Tom
  • 26,212
  • 21
  • 100
  • 111
  • I am not familiar with any PagingAndSortingRepository in JPA. If this is a Spring Data question then please indicate this is the case. – Alan Hay Nov 20 '14 at 08:23

10 Answers10

63

If you are talking about a Spring Data PagingAndSortingRepository you can set the default page size by using the @PageableDefault on a Controller method as follows:

public String listClients(@ModelAttribute FilterForm form, Model model, WebRequest request, @PageableDefault(sort = { "surname",
            "forename", "address.town" }, value = 50) Pageable pageable) {

    }

Or you can configure a global default using the following in your Spring config as shown below in both XML and Java config.

Note that newer versions of Spring Data use zero based page indexing while older versions used 1 for the first page. If your UI paging library expects 1 as first page then you can set the oneIndexedParameters property to true:

Configures whether to expose and assume 1-based page number indexes in the request parameters. Defaults to false, meaning a page number of 0 in the request equals the first page. If this is set to true, a page number of 1 in the request will be considered the first page.

Parameters: oneIndexedParameters - the oneIndexedParameters to set

Configures the Pageable to be used as fallback in case no PageableDefault or PageableDefaults (the latter only supported in legacy mode) can be found at the method parameter to be resolved. If you set this to null, be aware that you controller methods will get null handed into them in case no Pageable data can be found in the request. Note, that doing so will require you supply bot the page and the size parameter with the requests as there will be no default for any of the parameters available.

Parameters: fallbackPageable - the Pageable to be used as general fallback.

In XML this looks like the following then:

<mvc:annotation-driven>
    <mvc:argument-resolvers>
        <bean class="org.springframework.data.web.PageableHandlerMethodArgumentResolver">
            <property name="oneIndexedParameters" value="true"/>
            <property name="fallbackPageable">
                <bean class="org.springframework.data.domain.PageRequest">
                    <constructor-arg name="page" value="1" />
                    <constructor-arg name="size" value="10" />
                </bean>
            </property>
        </bean>
    </mvc:argument-resolvers>
</mvc:annotation-driven>

In Java Config this looks like the below:

@Configuration
public class WebConfig extends WebMvcConfigurerAdapter {


    @Override
    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
        PageableHandlerMethodArgumentResolver resolver = new PageableHandlerMethodArgumentResolver();
        resolver.setOneIndexedParameters(true);
        resolver.setFallbackPageable(new PageRequest(1, 20));
        argumentResolvers.add(resolver);
        super.addArgumentResolvers(argumentResolvers);
    }
}
Manuel Jordan
  • 15,253
  • 21
  • 95
  • 158
Alan Hay
  • 22,665
  • 4
  • 56
  • 110
  • Thanks, Alan. But it is rather name="page" value="0" for the global config, isn't it? – Mikhail Apr 19 '16 at 09:38
  • Yes. There was a change from 1 to 0 based index at some point although you can still override this to use the former if required. I will update when I have a minute. – Alan Hay Apr 19 '16 at 10:13
  • 1
    The Java Config has been depreciated as of Spring 5.x – CodeMonkey Apr 02 '18 at 18:46
  • Your statement that newer versions of Spring Data use zero based page indexing while older versions used 1 for the first page was very informative. I had been fighting with this one https://jira.spring.io/browse/DATACMNS-563 – Stephane Nov 26 '18 at 13:44
  • @All - Please guide here: https://stackoverflow.com/questions/56786158/unable-to-override-defaults-of-pagination-in-spring-boot-2-1-5-release – PAA Jun 27 '19 at 10:51
46

For Spring Boot 2.X you have set of parameters:

# DATA WEB (SpringDataWebProperties)
spring.data.web.pageable.default-page-size=20         # Default page size.
spring.data.web.pageable.max-page-size=2000           # Maximum page size to be accepted.
spring.data.web.pageable.one-indexed-parameters=false # Whether to expose and assume 1-based page number indexes.
spring.data.web.pageable.page-parameter=page          # Page index parameter name.
spring.data.web.pageable.prefix=                      # General prefix to be prepended to the page number and page size parameters.
spring.data.web.pageable.qualifier-delimiter=_        # Delimiter to be used between the qualifier and the actual page number and size properties.
spring.data.web.pageable.size-parameter=size          # Page size parameter name.
spring.data.web.sort.sort-parameter=sort              # Sort parameter name.
Jin Kwon
  • 20,295
  • 14
  • 115
  • 184
Przemek Nowak
  • 7,173
  • 3
  • 53
  • 57
  • 1
    I am using spring 2.0.2 and I can't use this parameters. Seems like it was a bug. https://github.com/spring-projects/spring-boot/issues/14413 – pitchblack408 Mar 18 '19 at 18:48
  • @pitchblack408 - Were you able to resolve this issue ? – PAA May 13 '19 at 08:47
  • @All - Look like this works only for the Spring Data REST HATEOAS – PAA May 13 '19 at 08:51
  • @pitchblack408 - In My case its not working. Could you please guide what did you used at bController other that Pageable ? – PAA Jun 27 '19 at 08:18
  • I think I wrote some code, I believe I created a bean and injected it. If you are still stuck on the problem, I can look for what I did and post it. Would that help? – pitchblack408 Jun 27 '19 at 18:15
  • 1
    Not working on 2.2.5.RELEASE @Bean PageableHandlerMethodArgumentResolverCustomizer pageableResolverCustomizer() { return pageableResolver ->{ pageableResolver.setMaxPageSize(500); }; } this worked for me but I'am not able to set default page size – ABDERRAHIME FARHANE Apr 16 '20 at 08:49
  • Worked for me in 2.2.6.RELEASE using `spring.data.web.pageable.default-page-size=2147483647` and `spring.data.web.pageable.max-page-size: 2147483647` together – Josh J May 12 '20 at 12:24
  • @Pra_A I did this programmatically, see my answer on a related post: https://stackoverflow.com/a/64011100/309261 – Neon Sep 22 '20 at 13:52
  • I am able to set these properties as mentioned in the updated answer here. `spring.data.web.pageable.default-page-size=20` I use Spring 2.2.6.RELEASE – Neeraj Singh Jul 13 '21 at 12:34
  • please is there any way to get the total size, I'm using my api endpoint in a frontend project and it's returning just 20 ? – hakima maarouf May 04 '22 at 11:54
21

You can set below in application.yml

spring.data.rest.default-page-size: 50
  • 2
    I have tried many ways of getting this setting to work and doesn't seem to want to take affect (1.4.3.RELEASE) -- have any suggestions? `spring.data.rest.default-page-size: 99` – Larchy Nov 18 '16 at 19:37
  • 3
    that setting is probably only relevant if you are using spring-data-rest HATEOS framework – dan carter Mar 06 '19 at 03:42
  • Its not throwing error if I set Page Size=60, how to handle these errors? – PAA Feb 07 '20 at 07:40
13

You can use this Annotation before your Pageable param:

@PageableDefault(size = 40)

// so your parameter should be like this:
@PageableDefault(size = 40) Pageable pageable

** Update: You can store 40 in application.yml file and use it in the whole project.

in application.yml :

pageSize: 40

then:

// so your parameter should be like this:
@PageableDefault(size = ${pageSize}) Pageable pageable
Hadi hashemi
  • 475
  • 6
  • 8
  • 3
    How to make this 40 configurable via properties file? – PAA May 13 '19 at 07:37
  • @PAA did you find a solution? – theprogrammer Jan 15 '20 at 20:31
  • You can put your local variables in application.yml file. ex: in application.yml file, I add this line: pageSize: 20. then I can use this in every controller file I need using : @Value("${pageSize}") private final int pageSize = 20; – Hadi hashemi Jan 22 '20 at 15:12
9

And, for completeness, here is an example for a Spring Boot configuration. In the @Configuration class that extends WebMvcConfigurerAdapter, set the default page size to 50 items like this:

@Override
public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
    PageableHandlerMethodArgumentResolver resolver = new PageableHandlerMethodArgumentResolver();
    resolver.setFallbackPageable(new PageRequest(0, 50));
    argumentResolvers.add(resolver);
    super.addArgumentResolvers(argumentResolvers);
}
Rori Stumpf
  • 1,912
  • 19
  • 26
3

This still isn't well documented but for anyone else finding this article, the RepositoryRestConfigurerAdapter has all the spring data rest config there.

@Configuration
public static class RestConfig extends RepositoryRestConfigurerAdapter {

    @Override
    public void configureRepositoryRestConfiguration(RepositoryRestConfiguration config) {
            config.setDefaultPageSize(50);
    }

}
jasondt
  • 33
  • 5
3

In Spring Boot 2.1.6.RELEASE, you can use below:

@Configuration
public class WebConfig implements WebMvcConfigurer{

    @Value("${paging.default.pageSize}")
    private int size;

    @Value("${paging.default.page}")
    private int page;


    @Override
    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> resolvers) {
        PageableHandlerMethodArgumentResolver resolver = new PageableHandlerMethodArgumentResolver();
        resolver.setFallbackPageable(PageRequest.of(page, size));
        resolvers.add(resolver);
        WebMvcConfigurer.super.addArgumentResolvers(resolvers);
    }
}
PAA
  • 1
  • 46
  • 174
  • 282
1

If there is someone who is trying to use @PageableDefault with spring.data.web.pageable.default-page-size and don't know why the default page size you set ins't working

public @interface PageableDefault {

    /**
     * The default-size the injected {@link org.springframework.data.domain.Pageable} should get if no corresponding
     * parameter defined in request (default is 10).
     */
    int size() default 10;

}

As you can see the above code if you use @PageableDefault the default page size is set to 10 no matter what you set spring.data.web.pageable.default-page-size: 99

So if you want to provide default sort condition in a simple way with page size , you can use something like this

@PageableDefault(size = 99, sort = {"createdAt"}, direction = sort.Direction.DESC) Pageable pageable
Dharman
  • 30,962
  • 25
  • 85
  • 135
0

Provided answers are very good and should help in most cases. However, I use slightly different approach, which allows me to use different default page size per model and can be configurable with Spring or system properties.

Please note that this approach has one fundamental limitation, namely, it does not accept any size coming with a request; it uses sorting information though. So if you need ability to change number of returned items per page via request parameters, this solution is not for you.

First of all, I created a utility class (or just a method in a controller) which creates a new Pageable instance base on a request Pageable and configured page size

public static Pageable updatePageable(final Pageable source, final int size)
{
    return new PageRequest(source.getPageNumber(), size, source.getSort());
}

In a controller I add a variable which holds my default page size (in this case default value is 20 if configuration is not provided):

@Value("${myapplication.model.items-per-page:20}")
private int itemsPerPage;

And then I override (i.e. create a new Pageable instance) default page size in request handling method:

@RequestMapping(method = RequestMethod.GET)
public Page<Model> websites(final Pageable pageable)
{
    return repository.findAll(updatePageable(pageable, itemsPerPage));
}

I use different default page size variables for different models / controllers which then can be configured even from system properties.

Tom
  • 26,212
  • 21
  • 100
  • 111
0

To check and validate if pageable param is not present in the request (e.g. http://ipServer:8080/getfiltered/ without "page" param). I dont need use the default-page-size , just need to set pageable to unpaged. This work for me (use size param instead default to check):

public ResponseEntity<List<Object>> getAllFiltered(@RequestParam(required = false) Integer size, Pageable pageable) {
        log.debug("REST request to get all obj filtered");

        if (pageable == null || size == null) {
            pageable = Pageable.unpaged();
        }

        return = service.findAllFiltered(pageable);
}
perezmirabile
  • 171
  • 2
  • 5