java - "spring.data.web.pageable.one-indexed-parameters=true"不起作用

标签 java spring spring-boot jpa spring-data

在我的 Spring Boot Rest 服务中,我想实现一个带有分页的 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;
}

最佳答案

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

情况 1: 默认行为是 spring.data.web.pageable.one-indexed-parameters=false 以及使用 发出请求时的行为http://localhost:8080/api/customerspage=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
}

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

关于java - "spring.data.web.pageable.one-indexed-parameters=true"不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52120070/

相关文章:

java+Swing : efficient overlay of rectangle or other "sprite"

ruby-on-rails - 将 ruby​​ 版本更新到 3.0.2 后,Rails 生成迁移不起作用

java - Maria DB 通信链接失败异常

spring - 在Docker上运行的Spring Cloud Netflix应用为什么被容器杀死了?

java - 可以将 Flux 转换为 Mono

java - 如何在没有显式调用的情况下调用 'actionPerformed' 方法?

java - android - 抽屉图标点击不起作用

configuration - 使用Spring Boot和Thymeleaf加载静态资源

java - 创建动态增长的数组所需的工作单元

java - 如何通过 maven-failsafe-plugin 运行基于 spring-boot 的应用程序的集成测试?