spring.data.rest.max-page-size似乎不起作用



在Spring Boot下1.3.0.M5我使用

spring.data.rest.max-page-size=10

应用程序中。属性.

但我仍然可以在URL中将大小设置为40,并得到正确的响应。例如:http://localhost:8080/cine20-春天/api/电影?page=0&size=40&sort=标题,asc

会还给我40部电影

那么这个参数有什么用呢?

使用Spring Boot 1.4.2更新测试

仍然存在一个问题:默认情况下,不带依赖项弹簧启动启动器数据休息,默认情况下,最大页面大小设置为2000并且更改最大页面大小值将不起作用:

spring.data.rest.max-page-size=0x7fffffff

添加弹簧启动启动器数据休息=>默认情况下,最大页面大小现在设置为1000,则更改参数最大页面大小将起作用:

spring.data.rest.max-page-size=0x7fffffff

我仍然相信,这是一种奇怪的行为。

在:https://github.com/spring-projects/spring-data-rest/blob/master/spring-data-rest-core/src/main/java/org/springframework/data/rest/core/config/RepositoryRestConfiguration.java

你可以找到private int maxPageSize = 1000;,它解释了为什么它变为1000。我还没有找到,为什么它从一开始就设置为2000。

我想在不需要添加依赖项的情况下自由设置参数spring.data.rest.max-page-sizespring-boot-starter-data-rest,但不幸的是,到目前为止我还没有找到方法。

这对我来说有点像是一个gotcha。我认为最简单的解决方案是使用配置属性而不是代码。这就是我发现的,也是对我有效的。

如果在@RestController中使用Pageable,则可以使用属性spring.data.web.pageable.max-page-size进行设置。

如果在@RepositoryRestResource中使用Pageable,则可以使用属性spring.data.rest.max-page-size进行设置。

我可以在Spring Boot 2.1.x和2.3.3上执行此操作,以覆盖默认的Pageable最大页面大小2000。

@Configuration
public class PageableConfig {
    @Bean
    PageableHandlerMethodArgumentResolverCustomizer pageableResolverCustomizer() {
        return pageableResolver -> pageableResolver.setMaxPageSize(9999);
    }
}

添加这些spring.data.web.pageable.max-page-sizespring.data.web.pageable.maxPageSize)中的任一个或application.properties中的spring.data.rest.max-page-sizespring.data.rest.maxPageSize)属性对我不起作用。即

# these properties did not allow me to override the default of 2000 for maxPageSize
spring.data.web.pageable.max-page-size=9999
spring.data.rest.max-page-size=9999

欲了解更多详细信息,请访问https://github.com/spring-projects/spring-boot/issues/14413这个https://jira.spring.io/browse/DATAREST-1290

请参阅相关SO:为JPA Pageable Object帖子设置默认页面大小:设置JPA可分页对象的默认页面大小

仅在通过代码强制执行配置的情况下为我工作

@Configuration
public class CustomizedRestMvcConfiguration extends RepositoryRestMvcConfiguration
{
  @Override
  public RepositoryRestConfiguration config() {
    RepositoryRestConfiguration config = super.config();
    config.setMaxPageSize(10000);
    return config;
  }
}

http://docs.spring.io/spring-data/rest/docs/2.4.0.RELEASE/reference/html/#_changing_the_base_uri

试试这个就行了在属性中添加此行

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

最新更新