Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to configure PageableHandlerMethodArgumentResolver in spring boot application

I have a spring boot application and I want to configure PageableHandlerMethodArgumentResolver, I try the following code, but doesn't not work:

@Configuration
public class WebConfig extends WebMvcConfigurerAdapter {

    // .......

    @Override
    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
        PageableHandlerMethodArgumentResolver resolver = new PageableHandlerMethodArgumentResolver();
        resolver.setFallbackPageable(new PageRequest(0, 50));
        argumentResolvers.add(resolver);
        super.addArgumentResolvers(argumentResolvers);
    }
}

Controller method:

@Override
@PreAuthorize("hasRole('ROLE_SYS_ADMIN')")
@RequestMapping(method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<Page<Account>> query(@QuerydslPredicate Predicate predicate,
        @PageableDefault Pageable pageable) {
    return new ResponseEntity<>(accountRepo.findAll(predicate, pageable), HttpStatus.OK);
}

And it is a simple Boot App, just playing with spring boot, with No Empty WebConfig Configuration class

@Configuration
public class WebConfig {

    @Bean
    public Filter html5ModeFilter() {
        return new Html5ModeFilter();
    }
}

And here's a list of my config classes:

Html5ModeFilter.java
SecurityConfig.java
ServletInitializer.java
UserDetailsAdapter.java
WebConfig.java
like image 715
Muhammad Hewedy Avatar asked Feb 14 '16 20:02

Muhammad Hewedy


People also ask

How do you make an object Pageable?

We can create a PageRequest object by passing in the requested page number and the page size. In Spring MVC, we can also choose to obtain the Pageable instance in our controller using Spring Data Web Support. The findAll(Pageable pageable) method by default returns a Page<T> object.

Which are the valid option to create Pageable instance?

The most common way to create a Pageable instance is to use the PageRequest implementation: Pageable pageable = PageRequest.

What is spring boot PageRequest?

Method SummaryReturns the Pageable requesting the first page. Sort. getSort()


2 Answers

Drop the @PageableDefault from the:

public ResponseEntity<Page<Account>> query(..., @PageableDefault Pageable pageable) {
    ...
}

PageableDefault annotation looks like this:

public @interface PageableDefault {
    int value() default 10;
    int size() default 10;
    int page() default 0;
    ...
} 

These default values are overriding your fallback value and you will see the first ten results.

like image 159
Ali Dehghani Avatar answered Oct 25 '22 08:10

Ali Dehghani


Starting in spring-data-commons version 2.0, there is are 2 new classes that will make this kind of thing easier:

  • SortHandlerMethodArgumentResolverCustomizer
  • PageableHandlerMethodArgumentResolverCustomizer

Unfortunately that's not the version that ships with the current version of Spring Boot (1.5.9), so replace at your own risk.

@Bean 
PageableHandlerMethodArgumentResolverCustomizer sortCustomizer() {
    // s is PageableHandlerMethodArgumentResolver
    return p -> p.setFallbackPageable(new PageRequest(0, 50));
}

This is just an alternative way to now set your fallback. @Ali Dehghani's answer still applies that the Default was overriding the Fallback

Spring Data Web Support

like image 36
Snekse Avatar answered Oct 25 '22 07:10

Snekse