Spring Data Rest - Configure pagination

后端 未结 2 1722
暗喜
暗喜 2020-12-18 14:45

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

相关标签:
2条回答
  • 2020-12-18 15:09

    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.

    0 讨论(0)
  • 2020-12-18 15:20

    I have configured the RequestMappingHandlerAdapter using a BeanPostProcessor, however I believe that's neither clean, nor elegant. That looks more like a hack. There must be a better way ! I'm giving the code below just for reference.

    public class RequestMappingHandlerAdapterCustomizer implements BeanPostProcessor {
    
        public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
            if (bean instanceof RequestMappingHandlerAdapter) {
                  RequestMappingHandlerAdapter adapter = (RequestMappingHandlerAdapter)bean;
                  List<HandlerMethodArgumentResolver> customArgumentResolvers = adapter.getCustomArgumentResolvers();
                  if(customArgumentResolvers != null) {
                      for(HandlerMethodArgumentResolver customArgumentResolver : customArgumentResolvers) {
                          if(customArgumentResolver instanceof HateoasPageableHandlerMethodArgumentResolver) {
                              HateoasPageableHandlerMethodArgumentResolver hateoasPageableHandlerMethodArgumentResolver = (HateoasPageableHandlerMethodArgumentResolver)customArgumentResolver;
                              hateoasPageableHandlerMethodArgumentResolver.setOneIndexedParameters(true);
                          }
                      }
                  }
            }
            return bean;
        }
    
        public Object postProcessBeforeInitialization(Object bean, String beanName)
                throws BeansException {
            return bean;
        }   
    
    }
    
    <beans ...>
      <bean class="util.spring.beanpostprocessors.RequestMappingHandlerAdapterCustomizer" />
    </beans>
    
    0 讨论(0)
提交回复
热议问题