将@PageableDefault与Spring Data REST一起使用

时间:2017-01-05 13:12:50

标签: spring spring-mvc pagination spring-data-rest

@PageableDefault的文档说:

  

注入时设置默认值的注释   org.springframework.data.domain.Pageable 进入控制器方法

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

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

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

2 个答案:

答案 0 :(得分:4)

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

答案 1 :(得分:0)

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

@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 描述的更一般选项进行例外处理时,这会派上用场。