Home > Software engineering >  pagination in quarkus backend and java
pagination in quarkus backend and java

Time:08-18

I'm new to quarkus and trying to get query result in quarkus using PanacheQuery.findAll().page( pageIndex,pageSize).list(); I run my query in data base and it returns correct data. but in my application it's always returns list of wrong numbers of objects.

what do I miss?

my database contains 6 objects in PatientsTbl when I set pageIndex to 1 and itemsPerPage to 6 (all objects in database) it returns no data when I set pageIndex to 1 and itemsPerPage to 4 it only returns 2!

@GET
@Path("getPag")
public Response getAll(@Path("/getPage/{pageIndex}/{itemsPerPage}")) {
    return Response.ok((productsRepository).findAll()
            .page(Page.of(pageIndex, itemsPerPage), pageRequest.getPageSize()))
            .list()).build();
}



in application :



 public List<PatientsTbl> getListWithPagination(int pageNum, int pageSize) throws Exception{
        String data = target.path("/patients/getPag").queryParam( "pageNum", pageNum ).queryParam( "pageSize", pageSize) .request(MediaType.APPLICATION_JSON).get(String.class);
        ObjectMapper mapper = new ObjectMapper();
        System.out.println(data);
    
        List<PatientsTbl> list = new ArrayList<>();
        list = mapper.readValue(data, new TypeReference<List<PatientsTbl>>(){});
        return list;
    
    }

CodePudding user response:

Look at the Javadoc of the Page#of method. It clearly says that the index is 0-based. So, when you use a pageIndex of 1, it will actually show you the second page.

  • Related