Spring Data Rest-配置分页 [英] Spring Data Rest - Configure pagination
问题描述
在2.1.0版中将Spring Data REST与JPA结合使用.
Using Spring Data REST with JPA in version 2.1.0.
如何配置分页以使page参数从索引1而不是0开始?
How can I configure the pagination in order to have the page argument starting at index 1 instead of 0 ?
我尝试用mvc:argument-resolvers
设置自定义HateoasPageableHandlerMethodArgumentResolver
,但这不起作用:
I have tried setting a custom HateoasPageableHandlerMethodArgumentResolver
with an mvc:argument-resolvers
, but that doesn't work:
<mvc:annotation-driven>
<mvc:argument-resolvers>
<bean class="org.springframework.data.web.HateoasPageableHandlerMethodArgumentResolver">
<property name="oneIndexedParameters" value="true"/>
</bean>
</mvc:argument-resolvers>
</mvc:annotation-driven>
请注意,此行为与mvc:argument-resolver
的文档完全一致:
Note that this behaviour is perfectly coherent with the documentation for mvc:argument-resolver
that says:
使用此选项不会覆盖内置的支持 解决处理程序方法参数.定制内置支持 对于参数解析,配置RequestMappingHandlerAdapter 直接.
Using this option does not override the built-in support for resolving handler method arguments. To customize the built-in support for argument resolution configure RequestMappingHandlerAdapter directly.
但是我怎么能做到这一点呢?如果可能的话,以干净优雅的方式?
But how can I achieve this ? If possible, in a clean and elegant way ?
推荐答案
最简单的方法是将RepositoryRestMvcConfiguration
子类化,并将您的类包含在配置中:
The easiest way to do so is to subclass RepositoryRestMvcConfiguration
and include your class into your configuration:
class CustomRestMvcConfiguration extends RepositoryRestMvcConfiguration {
@Override
@Bean
public HateoasPageableHandlerMethodArgumentResolver pageableResolver() {
HateoasPageableHandlerMethodArgumentResolver resolver = super.pageableResolver();
resolver.setOneIndexedParameters(true);
return resolver;
}
}
在您的XML配置中,替换:
In your XML configuration, replace:
<bean class="….RepositoryRestMvcConfiguration" />
使用
<bean class="….CustomRestMvcConfiguration" />
或在JavaConfig文件中导入自定义类,而不是标准类.
or import the custom class instead of the standard one in your JavaConfig file.
这篇关于Spring Data Rest-配置分页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!