爪哇:19
Springboot:3.0
依赖性:
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-cassandra</artifactId>
</dependency>
资料库:
PersonRepository extends ReactiveCassandraRepository<Person, Integer> {
Mono<Slice<Person>> findAll(Pageable page);
Mono<Slice<Person>> findAllByType(String type, Pageable page);
}
服务:
public Mono<Slice<Person>> findPaginatedPerson(int limit, int offset) {
int page = offset / limit;
//only working with page = 0
Pageable pageRequest = CassandraPageRequest.of(page,limit);
Mono<Slice<Person>> persons = personRepository.findAll(pageRequest);
//working for first page only
//Mono<Slice<Person>> persons = personRepository.findAll(CassandraPageRequest.first(1));
return persons;
}
控制器:
@GetMapping("/persons")
public List<Person> findPaginated(@RequestParam int limit, @RequestParam int offset) {
Mono<Slice<Person>> personPage = personService.findPaginatedPerson(limit, offset);
return personPage.block().getContent();
}
paagination 仅适用于第一页,下一页出现错误“无法为第一页以外的索引页面创建 Cassandra 页面请求 (0)”
有一个相关的answer,但没有提供足够的信息对我有帮助。
我试过像这样直接使用 Slice
public List<Person> findFoo(int limit, int offset) {
int page = offset / limit;
int currpage = 0, size = 2;
Pageable pageRequest = CassandraPageRequest.of(size, limit);
Slice< Person > persons = personRepository.findAllByAssetType(pageRequest);
while(persons.hasNext() && currpage < page) {
persons = personRepository.findAllByAssetType(persons.nextPageable());
currpage++;
}
return persons.getContent();
}
但这也给出了同样的错误。