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
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.
The most common way to create a Pageable instance is to use the PageRequest implementation: Pageable pageable = PageRequest.
Method SummaryReturns the Pageable requesting the first page. Sort. getSort()
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With