Set default page size for JPA Pageable Object

2020-01-29 06:53发布

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

8条回答
Bombasti
2楼-- · 2020-01-29 07:10

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

}
查看更多
不美不萌又怎样
3楼-- · 2020-01-29 07:13

You can set below in application.yml

spring.data.rest.default-page-size: 50
查看更多
Melony?
4楼-- · 2020-01-29 07: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
查看更多
啃猪蹄的小仙女
5楼-- · 2020-01-29 07:16

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);
    }
}
查看更多
地球回转人心会变
6楼-- · 2020-01-29 07:17

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);
}
查看更多
一纸荒年 Trace。
7楼-- · 2020-01-29 07:20

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.

查看更多
登录 后发表回答