When I hit the database with PagingAndSortingRepository.findAll(Pageable)
I get Page
. However, I want to expose DTO\'s to the clien
At the end, you will not return the Page to the users, but a list of ObjectDTO, with the Page details at the header, so this would be my solution.
public Page findAll (Pageable pageable){
//logic goes here.
Page page = objectRepository.findAll(pageable);
return page;
}
@GetMapping
public ResponseEntity> findAll (Pageable pageable){
Page page = objectServiceService.findAll(pageable);
HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(page, "your-endpoint-here");
return new ResponseEntity<>(objectMapper.toDto(page.getContent()), headers, HttpStatus.OK);
}
The reason for using this is so that you don't need to duplicate the page details for ObjectEntity and DTO. It is key to note that a page contains the following:
The content is the list of objects returned, and is the only thing that needs to be mapped to DTO.