问题
in my Spring Boot Rest Service I want to implement a getAll method with pagination for lazy loading in frontend later.
At the moment I have to request with page 0 if I want the first set of rows. With the following config inserted in the application.properties it should work... spring.data.web.pageable.one-indexed-parameters=true ... but it doesn't.
Does anybody knows why or is this a legacy way? I'm using spring-boot-starter-web and data-jpa in version 2.0.4.RELEASE.
Thanks a lot!
edit, here is the service method, maybe PageRequest can't handle this.
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;
}
回答1:
i think that is bug . see https://github.com/spring-projects/spring-boot/issues/14413
'SpringDataWebAutoConfiguration'should before'RepositoryRestMvcAutoConfiguration',this makes 'PageableHandlerMethodArgumentResolverCustomizer' not work ,so cofig yml ‘spring.data.web.pageable’ not work
回答2:
You need to add paging support to your Repositories, you need to extend the
PagingAndSortingRepository<T,ID>
interface rather than the basic
CrudRepository<T,ID>
interface. This adds methods that accept a Pageable to control the number and page of results returned.
public Page findAll(Pageable pageable);
Check it here https://docs.spring.io/spring-data/rest/docs/2.0.0.M1/reference/html/paging-chapter.html
回答3:
@Configuration
public class PageableConfig {
@Bean
PageableHandlerMethodArgumentResolverCustomizer pageableResolverCustomizer() {
return pageableResolver -> pageableResolver.setOneIndexedParameters(true);
}
}
来源:https://stackoverflow.com/questions/52120070/spring-data-web-pageable-one-indexed-parameters-true-does-not-work