Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Optional pagination with Spring

I am pretty new to java and spring.

What I want to implement is api endpoint /tickets with pagination and sorting. I made it and it works. But also what I would like to do is to return plain list of all tickets if size and page are not specified in the query params, so in FE I can use that list in selectbox.

What I have tried to do is to implement getTickets on service facade and return list of all tickets. But I didn't find a way how to check if Pageable is set as it always returns default values (size=20, page=0)

//Controller

@RequestMapping(method = RequestMethod.GET)
public ResponseEntity<Page<TicketListItemResponseModel>> getTickets(Pageable pageable) {
    logger.info("> getTickets");
    Page<TicketListItemResponseModel> tickets = ticketServiceFacade.getTickets(pageable);
    logger.info("< getTickets");
    return new ResponseEntity<>(tickets, HttpStatus.OK);
}

//TicketServiceFacade

public Page<TicketListItemResponseModel> getTickets(Pageable pageable) {
    Page<Ticket> tickets = ticketService.findAll(pageable);
    return tickets.map(new ConverterFromPagesToListItem());
}

public List<TicketListItemResponseModel> getTickets() {
    List<Ticket> tickets = ticketService.findAll();
    return tickets.stream()
            .map(t -> modelMapper.map(t, TicketListItemResponseModel.class))
            .collect(Collectors.toList());
}

Perhaps I do it totally wrong?

like image 207
Ruben Nagoga Avatar asked Nov 15 '16 21:11

Ruben Nagoga


People also ask

How do you implement pagination in Spring MVC?

Now for the implementation. The Spring MVC Controller for pagination is straightforward: In this example, we're injecting the two query parameters, size and page, in the Controller method via @RequestParam. Alternatively, we could have used a Pageable object, which maps the page, size, and sort parameters automatically.

What is Spring Boot pagination?

Spring Boot pagination tutorial shows how to paginate data in a Spring application. Spring is a popular Java application framework and Spring Boot is an evolution of Spring that helps create stand-alone, production-grade Spring based applications easily.

What is pagination in spring data JPA?

1. Overview Pagination is often helpful when we have a large dataset and we want to present it to the user in smaller chunks. Also, we often need to sort that data by some criteria while paging. In this tutorial, we'll learn how to easily paginate and sort using Spring Data JPA.

How to use pageable repository in spring data?

In Spring Data, if we need to return a few results from the complete data set, we can use any Pageable repository method, as it will always return a Page. The results will be returned based on the page number, page size, and sorting direction. Spring Data REST automatically recognizes URL parameters like page, size, sort etc.


1 Answers

If you build out your controller method like so, you can manage whether or not you want to implement paging by checking the request params:

@Override
public ResponseEntity<Page<TicketListItemResponseModel>> getTickets(
        @RequestParam(value = "page", defaultValue = "0", required = false) int page,
        @RequestParam(value = "count", defaultValue = "10", required = false) int size,
        @RequestParam(value = "order", defaultValue = "ASC", required = false) Sort.Direction direction,
        @RequestParam(value = "sort", defaultValue = "name", required = false) String sortProperty) {
    // here you would check your request params and decide whether or not to do paging and then return what you need to return
}

If you need to build a PageRequest to pass into your service method, you can do so manually like so:

new PageRequest(page, size, new Sort(direction, sortProperty));
like image 173
Gregg Avatar answered Oct 04 '22 01:10

Gregg