My repository method is:
public Page<Order> findByStatusIn(List<OrderStatus> orderStatuses, Pageable pageable);
Which is called from Controller (@RepositoryRestController
) as :
Pageable paging = PageRequest.of(page, pageSize, Sort.by(Sort.Direction.DESC,"id"));
Page<Order> myOrders = orderRepository.findByStatusIn(orderStatuses, paging);
The Page is converted to CollectionModel
:
CollectionModel<Order> resources = CollectionModel.of(myOrders);
resources.add(linkTo(methodOn(OrderController.class).userOrders(page,pageSize,currentUser)).withSelfRel());
return new ResponseEntity<>(resources,HttpStatus.OK);
The output json is like:
{
"_embedded": {
"orders": [
{
"orderId": 10011,
"createdAt": "2022-05-18T16:28:19+05:30",
"lastUpdatedAt": "2022-06-10T16:28:15+05:30",
"createdBy": "User01",
"status": "PENDING"
},
{
"orderId": 10012,
"createdAt": "2017-05-03T14:28:19+05:30",
"lastUpdatedAt": "2022-06-10T16:28:15+05:30",
"createdBy": "User01",
"status": "SHIPPED"
}
]
},
"_links": {
"self": {
"href": "http://localhost:8080/orders/userOrders?page=0&pageSize=11"
}
}
}
The data I got is correct but the response is missing the Page information as from the response getting from repository methods in Spring data. ie, the section like
"page": {
"size": 20,
"totalElements": 20,
"totalPages": 1,
"number": 0
}
is missing.
How the page information can be added to the response when @RepositoryRestController
is used?
PagedModel
should be used to create representations of pageable collections, instead of CollectionModel
. To easily convert Page
instances into PagedModel
use PagedResourcesAssembler
as described here.