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.
如何配置分页以使页面参数从索引 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屋!