0

i have problem. I have method @GetMapping for gets all requests from database, but when i want to set limit of records from DB pagination stops working, where i got something wrong ? Without line pageable = PageRequest.... it works fine.

@GetMapping("/requests")
    public ResponseEntity<List<Request>> getAllRequests(RequestCriteria criteria, Pageable pageable) {
        log.debug("REST request to get Requests by criteria: {}", criteria);
        pageable = PageRequest.of(0,recordsLimit);
        Page<Request> page = requestQueryService.findByCriteria(criteria,pageable);
        HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(ServletUriComponentsBuilder.fromCurrentRequest(), page);
        return ResponseEntity.ok().headers(headers).body(page.getContent());
    }
TomaszC283
  • 17
  • 6
  • 1
    It breaks because you are breaking it yourself. You are throwing away the pageable and create a new one always starting from the beginning. That will ofcourse break. – M. Deinum Jul 27 '20 at 06:47
  • is there other way to make limit in java rest ? – TomaszC283 Jul 27 '20 at 07:11
  • The pageable you receive should already contain the limit, why are you setting it again? – M. Deinum Jul 27 '20 at 07:22
  • no, it doesnt contain the limit :) i load 100k + records without PageRequest.of – TomaszC283 Jul 27 '20 at 10:08
  • Then what is the point of letting the user submit a page request if it doesn't contain the necessary data... To correctly specify the defaults see https://stackoverflow.com/questions/27032433/set-default-page-size-for-jpa-pageable-object or if you really want to stickto your code, replace the `0` with the actual selected page by the user (from the `PageRequest` that is passed in ). – M. Deinum Jul 27 '20 at 10:11

0 Answers0