Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set default page size for JPA Pageable Object

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

like image 565
Kousick Shanmugam Nagaraj Avatar asked Nov 20 '14 05:11

Kousick Shanmugam Nagaraj


People also ask

What is offset Pageable?

Offset-based pagination is a very famous technique wherein the client requests parameters with a specific limit (the number of results) and offset (the number of records that need to be skipped). Offset-based pagination is easy to use and is preferred for static data.

What is Pageable in Spring JPA?

Pagination and Filter with Spring Data JPA PagingAndSortingRepository extends CrudRepository to provide additional methods to retrieve entities using the pagination abstraction. findAll(Pageable pageable) : returns a Page of entities meeting the paging condition provided by Pageable object.

What is a Pageable object?

public interface Pageable. The Pageable implementation represents a set of pages to be printed. The Pageable object returns the total number of pages in the set as well as the PageFormat and Printable for a specified page.


2 Answers

If you are talking about a Spring Data PagingAndSortingRepository you can set the default page size by using the @PageableDefault on a Controller method as follows:

public String listClients(@ModelAttribute FilterForm form, Model model, WebRequest request, @PageableDefault(sort = { "surname",             "forename", "address.town" }, value = 50) Pageable pageable) {      } 

Or you can configure a global default using the following in your Spring config as shown below in both XML and Java config.

Note that newer versions of Spring Data use zero based page indexing while older versions used 1 for the first page. If your UI paging library expects 1 as first page then you can set the oneIndexedParameters property to true:

  • public void setOneIndexedParameters(boolean oneIndexedParameters)

Configures whether to expose and assume 1-based page number indexes in the request parameters. Defaults to false, meaning a page number of 0 in the request equals the first page. If this is set to true, a page number of 1 in the request will be considered the first page.

Parameters: oneIndexedParameters - the oneIndexedParameters to set

  • public void setFallbackPageable(Pageable fallbackPageable)

Configures the Pageable to be used as fallback in case no PageableDefault or PageableDefaults (the latter only supported in legacy mode) can be found at the method parameter to be resolved. If you set this to null, be aware that you controller methods will get null handed into them in case no Pageable data can be found in the request. Note, that doing so will require you supply bot the page and the size parameter with the requests as there will be no default for any of the parameters available.

Parameters: fallbackPageable - the Pageable to be used as general fallback.

In XML this looks like the following then:

<mvc:annotation-driven>     <mvc:argument-resolvers>         <bean class="org.springframework.data.web.PageableHandlerMethodArgumentResolver">             <property name="oneIndexedParameters" value="true"/>             <property name="fallbackPageable">                 <bean class="org.springframework.data.domain.PageRequest">                     <constructor-arg name="page" value="1" />                     <constructor-arg name="size" value="10" />                 </bean>             </property>         </bean>     </mvc:argument-resolvers> </mvc:annotation-driven> 

In Java Config this looks like the below:

@Configuration public class WebConfig extends WebMvcConfigurerAdapter {       @Override     public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {         PageableHandlerMethodArgumentResolver resolver = new PageableHandlerMethodArgumentResolver();         resolver.setOneIndexedParameters(true);         resolver.setFallbackPageable(new PageRequest(1, 20));         argumentResolvers.add(resolver);         super.addArgumentResolvers(argumentResolvers);     } } 
like image 124
Alan Hay Avatar answered Sep 23 '22 12:09

Alan Hay


For Spring Boot 2.X you have set of parameters:

# DATA WEB (SpringDataWebProperties) spring.data.web.pageable.default-page-size=20         # Default page size. spring.data.web.pageable.max-page-size=2000           # Maximum page size to be accepted. spring.data.web.pageable.one-indexed-parameters=false # Whether to expose and assume 1-based page number indexes. spring.data.web.pageable.page-parameter=page          # Page index parameter name. spring.data.web.pageable.prefix=                      # General prefix to be prepended to the page number and page size parameters. spring.data.web.pageable.qualifier-delimiter=_        # Delimiter to be used between the qualifier and the actual page number and size properties. spring.data.web.pageable.size-parameter=size          # Page size parameter name. spring.data.web.sort.sort-parameter=sort              # Sort parameter name. 
like image 27
Przemek Nowak Avatar answered Sep 23 '22 12:09

Przemek Nowak