Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

json - Spring Data Rest - Configure pagination

Using Spring Data REST with JPA in version 2.1.0.

How can I configure the pagination in order to have the page argument starting at index 1 instead of 0 ?

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>

Note that this behaviour is perfectly coherent with the documentation for mvc:argument-resolver that says:

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 ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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;
  }
}

In your XML configuration, replace:

<bean class="….RepositoryRestMvcConfiguration" />

with

<bean class="….CustomRestMvcConfiguration" />

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...