I have a PagingandSorting Repository which has a method that accecpts a pageable object. I also have a controller that accepts a pageable object through the URL.
My use case is that, if a user specifies a page size parameter in the URL i must take that value for the pageable object. If he does not mention take a default value of 50.
But the pageable object defaults to 20 right now.
Any Suggestions would help
This still isn't well documented but for anyone else finding this article, the RepositoryRestConfigurerAdapter has all the spring data rest config there.
You can set below in application.yml
You can use this Annotation before your Pageable param:
** Update: You can store 40 in application.yml file and use it in the whole project.
in application.yml :
then:
In Spring Boot
2.1.6.RELEASE
, you can use below:And, for completeness, here is an example for a Spring Boot configuration. In the @Configuration class that extends WebMvcConfigurerAdapter, set the default page size to 50 items like this:
Provided answers are very good and should help in most cases. However, I use slightly different approach, which allows me to use different default page size per model and can be configurable with Spring or system properties.
Please note that this approach has one fundamental limitation, namely, it does not accept any size coming with a request; it uses sorting information though. So if you need ability to change number of returned items per page via request parameters, this solution is not for you.
First of all, I created a utility class (or just a method in a controller) which creates a new
Pageable
instance base on a requestPageable
and configured page sizeIn a controller I add a variable which holds my default page size (in this case default value is 20 if configuration is not provided):
And then I override (i.e. create a new
Pageable
instance) default page size in request handling method:I use different default page size variables for different models / controllers which then can be configured even from system properties.