spring data jpa limit pagesize, how to set to maxSize

后端 未结 12 2493
自闭症患者
自闭症患者 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:53

    In conjunction with Spring MVC you can use PageableDefaults annotation with value = Integer.MAX_VALUE like

    public String showUsers(Model model, 
      @PageableDefaults(pageNumber = 0, value = Integer.MAX_VALUE) Pageable pageable) { … }
    

    see PageableDefaults annotation Javadoc.

    In any other client code you can set second constructor parameter to Integer.MAX_VALUE:

    new PageRequest(
        queryForm.getPageNumber()- 1, 
        queryForm.getPageSize() == null ? Integer.MAX_VALUE : queryForm.getPageSize(),
        Sort.Direction.ASC,"id"); 
    

    see PageRequest constructor. I assume that queryForm.getPageSize() is a wrapper type not a primitive. Otherwise you get a zero if pageSize wasn't set by the user (intentionally for a "search all" request).

    UPDATE:

    Since Spring Data Commons 1.6 you should use PageableDefault instead of PageableDefaults

    public String showUsers(Model model, 
        @PageableDefault(page= 2 ,value = Integer.MAX_VALUE)
    

    See PageableDefault annotation Javadoc.

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

    If you need per-controller limits like this:

    @RequestMapping(path = "/users")
    public ModelAndView users(@PageableLimits(maxSize = 10) Pageable pageable) {
        ...
    }
    
    @RequestMapping(path = "/comments")
    public ModelAndView comments(@PageableLimits(maxSize = 100) Pageable pageable) {
        ...
    }
    

    It can be done with custom annotation:

    @Documented
    @Retention(RetentionPolicy.RUNTIME)
    @Target(ElementType.PARAMETER)
    public @interface PageableLimits {
        int maxSize() default Integer.MAX_VALUE;
    
        int minSize() default 0;
    }
    

    And extended PageableHandlerMethodArgumentResolver configuration:

    @Configuration
    public class MvcConfig extends WebMvcConfigurerAdapter {
    
        @Override
        public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
            PageableHandlerMethodArgumentResolver resolver = new PageableHandlerMethodArgumentResolver() {
    
                @Override
                public Pageable resolveArgument(MethodParameter methodParameter, @Nullable ModelAndViewContainer mavContainer,
                                                NativeWebRequest webRequest, @Nullable WebDataBinderFactory binderFactory) {
                    Pageable p = super.resolveArgument(methodParameter, mavContainer, webRequest, binderFactory);
                    return getLimitsFromAnnotation(p, methodParameter);
                }
    
                private Pageable getLimitsFromAnnotation(Pageable p, MethodParameter methodParameter) {
    
                    PageableLimits limits = methodParameter.getParameterAnnotation(PageableLimits.class);
    
                    if (limits == null) return p;
    
                    if (p.getPageSize() > limits.maxSize())
                        return PageRequest.of(p.getPageNumber(), limits.maxSize(), p.getSort());
                    else if (p.getPageSize() < limits.minSize())
                        return PageRequest.of(p.getPageNumber(), limits.minSize(), p.getSort());
    
                    return p;
                }
            };
    
            resolver.setMaxPageSize(Integer.MAX_VALUE);
            argumentResolvers.add(resolver);
            super.addArgumentResolvers(argumentResolvers);
        }
    }
    
    0 讨论(0)
  • 2020-12-15 20:53

    As of Spring Data 2.0 you can also do the following:

    @Configuration
    public class WebConfiguration implements PageableHandlerMethodArgumentResolverCustomizer {
    
        @Override
        public void customize(PageableHandlerMethodArgumentResolver pageableResolver) {
            pageableResolver.setFallbackPageable(Pageable.unpaged());
        }
    
    }
    
    0 讨论(0)
  • 2020-12-15 20:54
    spring.data.rest.max-page-size=1000
    

    use this property in application.property and assign a higher value.

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

    In newer versions of spring-data (since v2.0), you can simply create a bean of org.springframework.data.web.config.PageableHandlerMethodArgumentResolverCustomizer and set PageableHandlerMethodArgumentResolver.setMaxPageSize.

    @Configuration
    public class WebMvcConfig {
    
        @Bean
        public PageableHandlerMethodArgumentResolverCustomizer paginationCustomizer() {
            return pageableResolver -> pageableResolver.setMaxPageSize(5);
        }
    }
    
    0 讨论(0)
  • 2020-12-15 20:58

    I use this in my code:

    public Page<MyObject> retrieveData(@RequestBody CustomParameters query, Pageable pageable, Sort sort) {
        if (pageable == null) {
            return new PageImpl<>(repository.findAll(query.buildSpecification(), sort));
        }
        return repository.findAll(query.buildSpecification(), pageable);
    }
    

    This has a couple of advantages. If you use this in controllers you can always define a Pageable and a Sort arguments, if the request contains a "page" parameter, a Pageable will be created automatically (if there is a "sort" parameter it will be included in the pageable if it's not null or in the Sort argument if it is).

    You can customize the sortResolver and pageableResolver in you configuration and then you have the same behaviour in all your controllers.

    This way when you send the request you can specify if you want just a number of records (sendig a "page" parameter, and "size") or if you want them all (not sending those parameters), but you always get the same return structure.

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