yeddez
yeddez

Reputation: 365

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.

Upvotes: 24

Views: 30891

Answers (3)

Nina
Nina

Reputation: 11

Dharman's answer helps me find out the reason. If you use pagination to get data from DB, and also you update the search field after you retrieve it, then the page info will get messed up.

Upvotes: -2

steven makunzva
steven makunzva

Reputation: 31

I had a similar issue and with mine it was due to the fact that I modify the data after doing a select such that the modified field is the same field I use for searching. This means each time I run the query it will match less and less records so page zero will always have new data, so in that instance using page 0 always will not hurt as new records will always fill page zero. In the other instance I did not modify the search field and my query will match the same records each time I execute my select statement so in this instance requesting the exact page was important.

Upvotes: 3

8bitjunkie
8bitjunkie

Reputation: 13245

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.

Upvotes: 86

Related Questions