在Spring Data REST中使用@PageableDefault

Nic*_*las 2 spring pagination spring-mvc spring-data-rest

@PageableDefault的文档说:

在将a org.springframework.data.domain.Pageable 注入控制器方法时,设置默认值的注释 。

当使用Spring Data REST时,有没有一种方法可以在不定义控制器的情况下设置默认值?

在如下所示的存储库中设置PageableDefault似乎不起作用。

Page<Player> findAll(@PageableDefault(size=5) Pageable pageable);
Run Code Online (Sandbox Code Playgroud)

ale*_*xbt 5

Spring和Spring-Boot的解决方案

您可以扩展RepositoryRestConfigurerAdapter配置以设置默认页面大小:

@Configuration
public class RepositoryRestConfig extends RepositoryRestConfigurerAdapter {

    @Override
    public void configureRepositoryRestConfiguration(RepositoryRestConfiguration repositoryRestConfiguration) {
            repositoryRestConfiguration.setDefaultPageSize(5);
    }
}
Run Code Online (Sandbox Code Playgroud)

仅适用于Spring-Boot的解决方案

您可以在application.properties中设置默认大小:

spring.data.rest.default-page-size=5
Run Code Online (Sandbox Code Playgroud)

其他Spring Data属性:

# 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.
Run Code Online (Sandbox Code Playgroud)

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