Antoniossss
Antoniossss

Reputation: 32507

Is there a way to restrict Pageable's size and page values passed to controller method by Spring?

I am using following controller method

public String listUsers(@PageableDefault(size = 20, page = 0) Pageable pageable)

And obviously, we can controll page and size with query params size and page. But lets say, we want to protect ourselfs from dumping whole database in one page by user or due to tempering. Is there a limit, that can be set to Pageable object eg. @DefaultPageableConstrains or I have to validate size by hand?

I just would like to make use of Spring validation mechanisms like @Valid @Min or @Max annotations.

Upvotes: 2

Views: 821

Answers (1)

Olantobi
Olantobi

Reputation: 879

The default maxPageSize is 2000. This can be overriden like so:

@Configuration
public class PaginationConfiguration extends SpringDataWebConfiguration {
    private final int MAX_PAGE_SIZE = 200;

    @Bean
    @Override
    public PageableHandlerMethodArgumentResolver pageableResolver() {
        PageableHandlerMethodArgumentResolver pageableHandlerMethodArgumentResolver =
            new PageableHandlerMethodArgumentResolver(sortResolver());

        pageableHandlerMethodArgumentResolver.setMaxPageSize(MAX_PAGE_SIZE);

        return pageableHandlerMethodArgumentResolver;
    }
}

Upvotes: 3

Related Questions