spring data jpa limit pagesize, how to set to maxSize

后端 未结 12 2492
自闭症患者
自闭症患者 2020-12-15 20:09

I have one requirement is to search by pageable and non-pageable,

and in my Java code, I use spring data jpa Pageable class,

 Pageable pageable = new         


        
相关标签:
12条回答
  • 2020-12-15 20:36
    pageable = PageRequest.of(0, Integer.MAX_VALUE);
    

    if overriding pageable based on some condition, better to not change sort, just pass on

    // pageable is coming from function
    if(!isPageable)
        pageable = PageRequest.of(0, Integer.MAX_VALUE, pageable.getSort());
    

    PageRequest's package org.springframework.data.domain.PageRequest

    0 讨论(0)
  • 2020-12-15 20:43

    Maybe you could use:

    @Override
    @GetMapping("/some")
    public ResponseEntity<Page<BaseEntityInformationDTO>> getTemplateInfo(
                  @PageableDefault(sort = "displayedName") Pageable pageable, 
                  @RequestParam(defaultValue = Integer.MAX_VALUE + "") Integer trueSize) {
        pageable = new PageRequest(pageable.getPageNumber, Math.max(pageable.getPageSize, trueSize),..)
        ....
    }
    
    0 讨论(0)
  • 2020-12-15 20:45

    Here is a working solution for Spring 5.x.x. You just need to add the default constructor. See the above example :

    @Configuration
    public class PaginationConfiguration extends SpringDataWebConfiguration {
    
        /**
         * @param context           must not be {@literal null}.
         * @param conversionService must not be {@literal null}.
         */
        public PaginationConfiguration(ApplicationContext context,
                                       @Qualifier("mvcConversionService") ObjectFactory<ConversionService> conversionService) {
            super(context, conversionService);
        }
    
        @Bean
        public PageableHandlerMethodArgumentResolver pageableResolver() {
            PageableHandlerMethodArgumentResolver pageableHandlerMethodArgumentResolver =
                    new PageableHandlerMethodArgumentResolver(sortResolver());
    
            pageableHandlerMethodArgumentResolver.setMaxPageSize(Integer.MAX_VALUE);
    
            return pageableHandlerMethodArgumentResolver;
        }
    
    }
    
    0 讨论(0)
  • 2020-12-15 20:45

    This works for me:

    All pageable request use MyCustomPage and use a default page and size if not found in request.

    If size (number of record exceed the max value) i set a default value, which in this case 50.

    public class MyCustomPage extends PageRequest{
        Integer MAX_PAGE_SIZE = 50
        //constructor 
        public MyCustomPage(Integer page, Integer size) {
            super(page,(size>MAX_PAGE_SIZE)?MAX_PAGE_SIZE:size);
        }
    }
    
    0 讨论(0)
  • 2020-12-15 20:48

    If you use Spring MVC this may help you. According to comments below this answer is fully correct for Spring 4.x.x and maybe ealier, but for Spring 5.x.x you probably need another solution.

    The first thing that you have to do is to use @PageableDefault annotation and set size to Integer.MAX_VALUE or any other value you want:

    public SomeResponseObject getSomething(
        @PageableDefault(size = Integer.MAX_VALUE) Pageable page
    ) {
        return someService.getSomething(page);
    }
    

    But it is not enough when your size value is very big (bigger than 2000 in Spring Data Core/spring-data-commons 1.12.3), because size will be still limited by maxPageSize variable in PageableHandlerMethodArgumentResolver class which is set by default to 2000. This was mentioned by pdorgambide. You will get something like this:

    As you can see there is size=2000 instead of expected size=2147483647 (size=Integer.MAX_VALUE).
    So the second step is to change mentioned maxPageSize. We can do it by overriding PageableHandlerMethodArgumentResolver. One of the ways to do it is to create a proper java configuration file in a proper package - if you use such kind of configuration in your project. You can do it also in xml config file if your project uses such. There is my solution (Java Spring Configuration File):

    @Configuration
    @EnableConfigurationProperties
    public class PaginationConfiguration extends SpringDataWebConfiguration {
    
        @Bean
        public PageableHandlerMethodArgumentResolver pageableResolver() {
            PageableHandlerMethodArgumentResolver pageableHandlerMethodArgumentResolver =
                new PageableHandlerMethodArgumentResolver(sortResolver());
    
            pageableHandlerMethodArgumentResolver.setMaxPageSize(Integer.MAX_VALUE);
    
            return pageableHandlerMethodArgumentResolver;
        }
    
    }
    

    Now everything works as expected:

    It is also worth to mention that in @PageableDefault annotation value is equivalent to size. In other words it is alias for size. It means that instead of size=Integer.MAX_VALUE you can write value=Integer.MAX_VALUE. What is more althought it makes no sense you can use both in the same time. If they are different only size is taken into consideration.

    0 讨论(0)
  • 2020-12-15 20:50

    For Spring 5.x.x, set spring.data.rest.max-page-size=2147483647 in application.properties.

    0 讨论(0)
提交回复
热议问题