Java &引用;spring.data.web.pageable.one索引参数=true“;不起作用
在我的SpringBootREST服务中,我想实现一个带有分页的getAll方法,以便以后在前端延迟加载 目前,如果需要第一组行,我必须请求第0页。在application.properties中插入以下配置后,它应该可以工作。。。 spring.data.web.pageable.one索引参数=true ... 但事实并非如此 有人知道为什么或者这是一种传统的方式吗?我在2.0.4.0版本中使用SpringBootStarterWeb和数据jpa 非常感谢 编辑,这是服务方法,可能PageRequest无法处理Java &引用;spring.data.web.pageable.one索引参数=true“;不起作用,java,spring,spring-boot,jpa,spring-data,Java,Spring,Spring Boot,Jpa,Spring Data,在我的SpringBootREST服务中,我想实现一个带有分页的getAll方法,以便以后在前端延迟加载 目前,如果需要第一组行,我必须请求第0页。在application.properties中插入以下配置后,它应该可以工作。。。 spring.data.web.pageable.one索引参数=true ... 但事实并非如此 有人知道为什么或者这是一种传统的方式吗?我在2.0.4.0版本中使用SpringBootStarterWeb和数据jpa 非常感谢 编辑,这是服务方法,可能PageR
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;
}
公共列表findAll(int pageNumber,int pageSize){
List transactionResponseDTOs=新建ArrayList();
PageRequest PageRequest=PageRequest.of(页码、页面大小);
列出交易实体=
transactionRepository.findAll(pageRequest).getContent();
for(交易实体交易实体:交易实体){
TransactionResponseTos.add(转换(transactionEntity));
}
返回TransactionResponseTos;
}
您需要向存储库添加分页支持,您需要扩展
PagingAndSortingRepository<T,ID>
在这里检查一下我认为这是错误。看
“SpringDataWebAutoConfiguration”应该在“RepositoryRestMvcAutoConfiguration”之前,这会使“PageableHandlerMethodArgumentResolverConsolutionsMizer”不起作用,所以cofig yml'spring.data.web.pageable'不起作用属性spring.data.web.pageable.one index parameters=true仅控制如何将分页参数自动绑定到web请求处理程序方法的pageable参数的行为
@Configuration
public class PageableConfig {
@Bean
PageableHandlerMethodArgumentResolverCustomizer pageableResolverCustomizer() {
return pageableResolver -> pageableResolver.setOneIndexedParameters(true);
}
}
案例1:默认行为,即spring.data.web.pageable.one-indexed parameters=false以及当请求是用?page=5发出时
@GetMapping(“/api/customers”)
公共列表getCustomers(可分页){
//此处为pageable.getPageNumber()==5
Page customersPage=customerRepository.findAll(可分页);
//这里CustomerPage.getNumber()==5
}
案例2:带有spring.data.web.pageable.one-indexed parameters=true且当使用
@GetMapping(“/api/customers”)
公共列表getCustomers(可分页){
//这里是pageable.getPageNumber()==4
Page customersPage=customerRepository.findAll(可分页);
//这里CustomerPage.getNumber()==4
}
请注意,一旦获得数据Page customersPage,如果选中customersPage.getNumber(),它只会返回pageable.getPageNumber()中的内容,即4。我们可能期望5希望一个索引参数使用基于1的索引返回5,但事实并非如此。因为O.p显然在使用PageRequest,所以可以很安全地假设他们已经在使用PagingAndSortingRepository,不是吗?抱歉,延迟了。不幸的是,您的解决方案也不起作用。分页的第一页仍然是0,而不是1。无论如何,谢谢你的支持!:)请为您的答案添加一些解释,以便其他人了解您的代码是如何工作的。在sdr中,SpringDataWebAutoConfiguration将在RepositoryRestMvcAutoConfiguration之后以及在缺少bean PageableHandlerMethodArgumentResolver时进行配置,但RepositoryRestMVCCConfiguration具有bean PageableHandlerMethodArgumentResolver by pageableResolver(),因此SpringDataWebProperties将无法工作。例如,方法pageableResolver调用方法customizePageableResolver,它可以由PageableHandlerMethodArgumentResolver配置为自定义PageableHandlerMethodArgumentResolver。这对我有效,我在现有配置类中编写。对我有效!谢谢
public Page findAll(Pageable pageable);
@Configuration
public class PageableConfig {
@Bean
PageableHandlerMethodArgumentResolverCustomizer pageableResolverCustomizer() {
return pageableResolver -> pageableResolver.setOneIndexedParameters(true);
}
}
@GetMapping("/api/customers")
public List<Customer> getCustomers(Pageable pageable) {
//here pageable.getPageNumber() == 5
Page<Customer> customersPage = customerRepository.findAll(pageable);
//here customersPage.getNumber() == 5
}
@GetMapping("/api/customers")
public List<Customer> getCustomers(Pageable pageable) {
//here pageable.getPageNumber() == 4
Page<Customer> customersPage = customerRepository.findAll(pageable);
//here customersPage.getNumber() == 4
}