Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use @PageableDefault with Spring Data REST

The documentation of @PageableDefault says:

Annotation to set defaults when injecting a org.springframework.data.domain.Pageable into a controller method.

When using Spring Data REST, is there a way to set default values without defining a controller ?

Setting PageableDefault in the repository like below doesn't seem to work.

Page<Player> findAll(@PageableDefault(size=5) Pageable pageable);
like image 992
Nicolas Avatar asked Jan 05 '17 13:01

Nicolas


1 Answers

Solution for Spring and Spring-Boot

You may extend RepositoryRestConfigurerAdapter configuration to set the default page size:

@Configuration
public class RepositoryRestConfig extends RepositoryRestConfigurerAdapter {

    @Override
    public void configureRepositoryRestConfiguration(RepositoryRestConfiguration repositoryRestConfiguration) {
            repositoryRestConfiguration.setDefaultPageSize(5);
    }
}

Solution for Spring-Boot only

You may set the default size in application.properties:

spring.data.rest.default-page-size=5

Other Spring Data properties:

# DATA REST (RepositoryRestProperties)
spring.data.rest.base-path= # Base path to be used by Spring Data REST to expose repository resources.
spring.data.rest.default-page-size= # Default size of pages.
spring.data.rest.detection-strategy=default # Strategy to use to determine which repositories get exposed.
spring.data.rest.enable-enum-translation= # Enable enum value translation via the Spring Data REST default resource bundle.
spring.data.rest.limit-param-name= # Name of the URL query string parameter that indicates how many results to return at once.
spring.data.rest.max-page-size= # Maximum size of pages.
spring.data.rest.page-param-name= # Name of the URL query string parameter that indicates what page to return.
spring.data.rest.return-body-on-create= # Return a response body after creating an entity.
spring.data.rest.return-body-on-update= # Return a response body after updating an entity.
spring.data.rest.sort-param-name= # Name of the URL query string parameter that indicates what direction to sort results.

source: https://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#appendix

like image 51
alexbt Avatar answered Oct 23 '22 13:10

alexbt