Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Data Pageable: Returns empty content

I'm new with Spring Data and Spring MVC and I don't understand why am I getting empty content:

@RequestMapping(value="/pages", method=RequestMethod.GET)
@ResponseBody 
public Page<Client> contactsPages(@RequestParam int page, @RequestParam int size) {
    Pageable pageable = new PageRequest(page, size, new Sort("id"));
    Page<Client> pageResult = clientRepository.findAll(pageable);

    return pageResult;
} 

The result of my json when I test the url is:

{"content":[],"last":true,"totalElements":2,"totalPages":1,"size":5,"number":1,"sort":[{"direction":"ASC","property":"id","ignoreCase":false,"nullHandling":"NATIVE","ascending":true}],"first":false,"numberOfElements":0}

And if you have good example making pageable request using Spring Data and Spring MVC and AngularJS; It will be a big help for me.

like image 518
yeddez Avatar asked Apr 14 '16 09:04

yeddez


1 Answers

Ensure that your PageRequest object is requesting 0 for small sets, not 1.

The pagination begins from 0.

This is a common mistake for beginners and is a common redherring when using @Query in conjunction with Spring pagination. If your @Query works without pagination and then returns nothing when using it, check the page number.

like image 107
8bitjunkie Avatar answered Oct 16 '22 17:10

8bitjunkie