在我的 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/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
}
请注意,一旦您获取数据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/