我想通过spring数据为cassandra db实现分页休息服务。我使用了CassandraRepository,CrudRepository
和PagingAndSortingRepository
。但是这些都没有帮助实现分页。每次我收到此错误。请帮帮我。
@Service
public class BookService {
@Autowired
private BookRepository repo; // it uses PagingAndSortingRepository
@Autowired
private BookPagingRepository bookPaginationRepo; // it uses CassandraRepository
public void save(Book book) {
repo.save(book);
}
public Book findByBookId(String id) {
Book book = repo.findById(id).isPresent() ? repo.findById(id).get() : new Book();
System.out.println(book.toString() + "\n" + book);
return book;
}
public Slice<Book> getBooks(Pageable page){
return bookPaginationRepo.findAll(page); //I tried CassandraRequest.first(3) also
}
BookPagingRepository.java
@Repository
public interface BookPagingRepository extends CassandraRepository<Book, String> {
}
BookEndpoint.java
@RestController
@RequestMapping(consumes = MediaType.APPLICATION_JSON_VALUE,produces = MediaType.APPLICATION_JSON_VALUE)
public class BookEndpoint {
@Autowired
private BookService service;
@RequestMapping(value="/book",method=RequestMethod.POST)
public Book saveBook(@RequestBody Book book) {
service.save(book);
return book;
}
@RequestMapping(value="/book/{bookid}",method=RequestMethod.GET,consumes=MediaType.ALL_VALUE)
public Book getBookbyID(@PathVariable("bookid") String bookid) {
Book book = service.findByBookId(bookid);
return book;
}
@RequestMapping(value="/books",method=RequestMethod.GET,
consumes=MediaType.ALL_VALUE)
public Map<String,Object> getBookbyID(Pageable page) {
CassandraPageRequest cpr = CassandraPageRequest.of(page.getPageNumber(), page.getPageSize());
List<Book> book = service.getBooks(cpr).getContent();
Map<String,Object> map = new HashMap<String,Object>();
map.put("books", book);
map.put("page", page.getPageNumber());
map.put("size", page.getPageSize());
return map;
}
}