简体   繁体   中英

Spring Data: Page information missing fron response on using CollectionModel with RepositoryRestController

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?

To create representations of pageable collections use PagedModel , instead of CollectionModel .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM