skyman
skyman

Reputation: 2335

How to page rerturned data in a GET request

I have the following mapping:

@RequestMapping(value = "/client/list", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<List<Client>> listAll(
        @RequestHeader(value = "username", required = true) String username,
        @RequestHeader(value = "api_key", required = false) String apiKey) {

    if (!authenticationService.validate(username, apiKey)){
        throw new UnauthorizedUserException();
    }

    List<Client> clients = clientService.findAll();

    return new ResponseEntity<List<Client>>(clients, HttpStatus.OK);
}

My issue is that many thousands of records may be returned. How would I best desing this mapping to support paging on the client?

Upvotes: 0

Views: 54

Answers (2)

CodeRunner
CodeRunner

Reputation: 149

You can make a class as your input object or request which takes all parameters and then if you have to apply the pagination by using the parameters. Please check below :

public class ClientSearchCriteria {
private String username;
private String apikey;
private Long pageNo;
private Long recordsPerPage;

/*getters and setters*/

}

And in you database call(if you are using hibernate and criteria): use the following method -

  public Criteria applyPaginationCriteria(ClientSearchCriteria searchCriteria){
    Long recordsPerPage = searchCriteria.getRecordsPerPage();
    if (recordsPerPage >= 0L) {
        criteria.setFirstResult((int) (searchCriteria.getPageNo() * recordsPerPage - recordsPerPage));
        criteria.setMaxResults(recordsPerPage.intValue());
    }
     return criteria;
}

or if other than hibernate call, you can use the sql query to retrieve the results (as above HQL query) and then pass it your service call.

Upvotes: 1

deepansh2323
deepansh2323

Reputation: 261

You can always use a JTable. See an example here

Upvotes: 0

Related Questions