将 @PageableDefault 与 Spring 数据 REST 一起使用

Use @PageableDefault with Spring Data REST

@PageableDefault 的文档说:

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

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

像下面这样在存储库中设置 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 的解决方案-仅启动

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

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

其他Spring数据属性:

# 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

当使用 RestResource 注释存储库方法时,可以通过拦截请求并添加默认参数值(如果 none 存在)来自定义页面大小:

@Component
@Order(1)
public class RestResourceDefaultPaginationFilter implements Filter {
    @Value("${rest.resource.custom.page.size}")
    private String pageSize;

    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        chain.doFilter(new HttpServletRequestWrapper((HttpServletRequest) request) {
            public String getParameter(String paramName) {
                String value = super.getParameter(paramName);
                // if no size parameter defined on request, then use the configuration default
                if ("size".equals(paramName) && StringUtils.isEmpty(value)) {
                    return page.size;
                }
                return value;
            }
        }, response);
    }
}

然后为特定 url 模式注册此过滤器:

@Configuration
public class RestResourcePaginationConfig {

    @Bean
    public FilterRegistrationBean<RestResourceDefaultPaginationFilter> paginationFilter() {
        FilterRegistrationBean<RestResourceDefaultPaginationFilter> registrationBean = new FilterRegistrationBean<>();
        registrationBean.setFilter(new RestResourceDefaultPaginationFilter());
        registrationBean.addUrlPatterns("/myEntities/search/rest-resource-endpoint");
        return registrationBean;
    }
}

当您想对 alexbt 描述的更通用的选项进行例外处理时,这会派上用场。