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