Reputation: 33
In db table products
I have 12 records. Ids from 1 to 12. I call db using JpaRepository which extends PagingAndSortingRepository. Can't understand:
Why Pageable doesn't return expected result (size)?
Also I can't get product with id:1. why it happens?
why pageble doesn't start from id:1, then 2 and etc?
Please advise.
API /getPageable/{page}/{size}
I call /getPageable/1/10
and get list of 2 elements
.
I call /getPageable/1/9
and get 3 elements
/getPageable/1/8 - 4 elements ids: 10, 11, 12, 5
/getPageable/1/7 - 5 elements ids: 9, 10, 11, 12, 5
/getPageable/1/6 - 6 elements ids: 8, 9, 10, 11, 12, 5
/getPageable/1/5 - 5 elements ids: 7, 8, 9, 10, 11
/getPageable/1/4 - 4 elements ids: 6, 7, 8, 9
/getPageable/1/3 - 3 elements ids: 4, 6, 7
/getPageable/1/2 - 2 elements ids: 3, 4
/getPageable/1/1 - 1 element id:2
/getPageable/2/1 - 1 element id: 3
/getPageable/2/2 - 2 elements ids: 6, 7
/getPageable/2/3 - 3 elements ids: 8, 9, 10
/getPageable/2/4 - 4 elements ids: 10, 11, 12, 5
/getPageable/2/5 - 2 elements ids: 12, 5
/getPageable/2/6 - 0 elements
/getPageable/2/7 - 0 elements
/getPageable/2/8 - 0 elements
Controller code:
@GET
@Path("/getPageable/{pageId}/{size}")
@Produces({"application/json"})
@ApiOperation(value = "Get randomly list of products")
@ApiResponses(value = {
@ApiResponse(code = 200, message = "OK"),
@ApiResponse(code = 500, message = "Something wrong in Server")})
@ApiImplicitParams({
@ApiImplicitParam(name = "X-Auth-Token", value = "Authorization token", required = true, dataType = "string", paramType = "header")
})
public List<Product> getProductsRandomly(@PathParam("pageId") Integer pageId, @PathParam("size") Integer size) {
Pageable pageable = new PageRequest(pageId, size);
return productDao.findPageable(pageable).getContent();
}
productDao
@Repository
public class ProductDao extends BaseDao<Product, Integer> {
@Autowired
private ProductRepository productRepository;
....
}
productResopitory
public interface ProductRepository extends JpaRepository<Product, Integer> {
...
}
JpaRepository extends PagingAndSortingRepository
which has method Page<T> findAll(Pageable var1);
Upvotes: 1
Views: 218
Reputation: 26
Try subtracting 1 into pageId. As database also start indexing with 0.
Pageable pageable = new PageRequest(pageId-1, size);
Upvotes: 1