I have a HotelRepository which has a named method which returns a default Page<Hotel>
item as result instead of a list.
I want to change the content type Hotel
to HotelDto
in the page since the DTO has customized parameter that I'd like to display. I have already a constructor to convert Hotel to HotelDto.
My attempt:
Page<Hotel> hotels = dao.findAll(pageRequest);
return new PageImpl<>(
hotels.getContent().stream()
.map(hotel -> new HotelListItemDto(hotel, hotel.getSupplier())).collect(Collectors.toList()),
pageRequest, hotels.getContent().size());
The problem is that it only manipulate one page of the result. Of course, I can get all the results as a list first then create a page based on the list, but it loses the advantage of Page
(I think returning page would improve the performance of the search request).
So what should I do to keep the page advantage but still have the ability to customize the output?
You almost did it :)
Unless I'm skipping something, the only thing you need to change is the size that you are passing to the PageImpl
constructor.
Instead of using hotels.getContent().size()
(gives you the size of the content on the actual page) you should use hotels.getTotalElements()
which gives you the total size of the elements in all the pages available.
Update 1:
To do it properly, you should change your code for:
Page<Hotel> hotels = dao.findAll(pageRequest);
return new PageImpl<>(
hotels.getContent().stream()
.map(hotel -> new HotelListItemDto(hotel, hotel.getSupplier())).collect(Collectors.toList()),
pageRequest, hotels.getTotalElements());
The code from above is going to create a page analog to the hotels
page. And this is how your parameters are enough for the PageImpl:
HotelListItemDemo
) that the new page will have. Since this is just a mapping for the hotels
content to another type, the number of elements will be exactly the same.hotels.getTotalElements()
method). With that, the PageImpl can also calculate things like hasNext()
that I mentioned in the comments.Hope this helps you.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With