gpt4 book ai didi

spring - 将 @PageableDefault 与 Spring Data REST 结合使用

转载 作者:行者123 更新时间:2023-12-01 17:55:46 24 4
gpt4 key购买 nike

@PageableDefault 的文档说:

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

使用 Spring Data REST 时,有没有办法在不定义 Controller 的情况下设置默认值?

像下面这样在存储库中设置 PageableDefault 似乎不起作用。

Page<Player> findAll(@PageableDefault(size=5) Pageable pageable);

最佳答案

Spring 和 Spring-Boot 的解决方案

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

@Configuration
public class RepositoryRestConfig extends RepositoryRestConfigurerAdapter {

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

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

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

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

其他 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.

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

关于spring - 将 @PageableDefault 与 Spring Data REST 结合使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41486047/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com