Optional pagination with Spring

I am new to java and spring.

What I want to implement is an api endpoint /ticketswith pagination and sorting. I did it and it works. But what I would like to do is return a simple list of all the tickets, if sizeand pageare not specified in the request parameters, so the FE I can use this list in the selectbox.

What I tried to do was implement getTicketsin the list of services and return a list of all tickets. But I did not find a way to check if Pageable is installed, since it always returns the 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 am doing this completely wrong?

+4
1

, , , :

@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
}

PageRequest, , :

new PageRequest(page, size, new Sort(direction, sortProperty));
+4

Source: https://habr.com/ru/post/1660857/


All Articles