【问题标题】:"spring.data.web.pageable.one-indexed-parameters=true" does not work“spring.data.web.pageable.one-indexed-parameters=true”不起作用
【发布时间】:2019-02-06 18:12:45
【问题描述】:

在我的 Spring Boot Rest Service 中,我想实现一个带有分页的 getAll 方法,以便稍后在前端延迟加载。

目前,如果我想要第一组行,我必须请求第 0 页。在 application.properties 中插入以下配置后,它应该可以工作... spring.data.web.pageable.one-indexed-parameters=true ...但事实并非如此。

有人知道为什么或者这是一种传统方式吗?我在 2.0.4.RELEASE 版本中使用 spring-boot-starter-web 和 data-jpa。

非常感谢!

编辑,这里是服务方法,可能PageRequest处理不了。

public List<TransactionResponseDTO> findAll(int pageNumber, int     pageSize) {

    List<TransactionResponseDTO> transactionResponseDTOs = new ArrayList<>();

    PageRequest pageRequest = PageRequest.of(pageNumber, pageSize);

    List<TransactionEntity> transactionEntities =
    transactionRepository.findAll(pageRequest).getContent();

    for (TransactionEntity transactionEntity : transactionEntities) {
        transactionResponseDTOs.add(convert(transactionEntity));
    }

    return transactionResponseDTOs;
}

【问题讨论】:

    标签: java spring spring-boot jpa spring-data


    【解决方案1】:

    您需要为您的存储库添加分页支持,您需要扩展

    PagingAndSortingRepository<T,ID>
    

    界面而非基础

    CrudRepository<T,ID> 
    

    界面。这会添加接受 Pageable 的方法来控制返回结果的数量和页面。

    public Page findAll(Pageable pageable);
    

    在这里查看https://docs.spring.io/spring-data/rest/docs/2.0.0.M1/reference/html/paging-chapter.html

    【讨论】:

    • 由于 O.P 显然在使用 PageRequest,因此可以很安全地假设他们已经在使用 PagingAndSortingRepository,不是吗?
    • 抱歉耽搁了。不幸的是,您的解决方案也不起作用。第一页的分页还是0而不是1。无论如何感谢支持! :)
    【解决方案2】:

    我认为这是错误。见https://github.com/spring-projects/spring-boot/issues/14413

    'SpringDataWebAutoConfiguration'应该在'RepositoryRestMvcAutoConfiguration'之前,这使得'PageableHandlerMethodArgumentResolverCustomizer'不起作用,所以cofig yml'spring.data.web.pageable'不起作用

    【讨论】:

      【解决方案3】:
      @Configuration
      public class PageableConfig {
      
          @Bean
          PageableHandlerMethodArgumentResolverCustomizer pageableResolverCustomizer() {
              return pageableResolver -> pageableResolver.setOneIndexedParameters(true);
          }
      }
      

      【讨论】:

      • 请在您的答案中添加一些解释,以便其他人知道您的代码是如何工作的。
      • 在 sdr 中,SpringDataWebAutoConfiguration 将在 RepositoryRestMvcAutoConfiguration 和缺少的 bean PageableHandlerMethodArgumentResolver 之后进行配置,但是 RepositoryRestMvcConfiguration 通过 pageableResolver() 具有 bean PageableHandlerMethodArgumentResolver,因此 SpringDataWebProperties 将不起作用。好在pageableResolver方法调用customizePageableResolver方法,可以通过PageableHandlerMethodArgumentResolverCustomizer配置自定义PageableHandlerMethodArgumentResolver。
      • 这对我有用,我在现有的配置类中编写。
      【解决方案4】:

      属性 spring.data.web.pageable.one-indexed-parameters=true 只控制如何将分页参数自动绑定到 Web 请求处理程序方法的 Pageable 的行为strong> 论据。

      案例 1: 默认行为是 spring.data.web.pageable.one-indexed-parameters=false 以及使用 发出请求时http://localhost:8080/api/customers?page=5

      @GetMapping("/api/customers")
      public List<Customer> getCustomers(Pageable pageable) {
         //here pageable.getPageNumber() == 5
         Page<Customer> customersPage = customerRepository.findAll(pageable);
         //here customersPage.getNumber() == 5
      }
      

      案例 2: 使用 spring.data.web.pageable.one-indexed-parameters=true 并且使用 http://localhost:8080/api/customers?page=5 发出请求时

      @GetMapping("/api/customers")
      public List<Customer> getCustomers(Pageable pageable) {
         //here pageable.getPageNumber() == 4
         Page<Customer> customersPage = customerRepository.findAll(pageable);
         //here customersPage.getNumber() == 4
      }
      

      请注意,一旦您获得数据 Page customersPage,如果您检查 customersPage.getNumber(),它只会返回 pageable.getPageNumber() 中的内容 即 4。我们可能期望 5 希望单索引参数会使用基于 1 的索引返回 5,但事实并非如此。

      【讨论】:

      • 我怎样才能得到 pageNumber 属性也给我一个基于索引的结果?
      猜你喜欢
      • 2012-10-07
      • 1970-01-01
      • 2014-11-30
      • 1970-01-01
      • 2014-04-17
      • 2013-06-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多