Spring Data Rest-配置分页

jpl*_*ain 2 rest json spring-mvc spring-data spring-data-rest

在2.1.0版中将Spring Data REST与JPA结合使用。

如何配置分页以使page参数从索引1而不是0开始?

我曾尝试设置自定义HateoasPageableHandlerMethodArgumentResolvermvc:argument-resolvers,但不工作:

<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>
Run Code Online (Sandbox Code Playgroud)

请注意,此行为与该文档完全一致mvc:argument-resolver

使用此选项不会覆盖对解析处理程序方法参数的内置支持。要自定义对参数解析的内置支持,请直接配置RequestMappingHandlerAdapter。

但是我该如何实现呢?如果可能,以一种干净优雅的方式?

Oli*_*ohm 5

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;
  }
}
Run Code Online (Sandbox Code Playgroud)

In your XML configuration, replace:

<bean class="….RepositoryRestMvcConfiguration" />
Run Code Online (Sandbox Code Playgroud)

with

<bean class="….CustomRestMvcConfiguration" />
Run Code Online (Sandbox Code Playgroud)

or import the custom class instead of the standard one in your JavaConfig file.

  • 这就像一个魅力。而且它比使用BeanPostProcessor麻烦。谢谢。 (2认同)