Creating Links with Additional Query Parameters Using PagedResourcesAssembler
I am using spring-data-common PagedResourcesAssembler in my REST controller and I was glad to see that it even generates the following / previous links in the response. However, in cases where I have additional query parameters (other than page, size, sorting), they are not included in the generated links. Is there any way I can customize the assembler to include parameters in links?
Thanks a lot Daniel
source to share
You need to create the base link yourself and pass it to the "toResource" method for the PagedResourcesAssembler.
@Controller
@RequestMapping(value = "/offer")
public class OfferController {
private final OfferService offerService;
private final OfferAssembler offerAssembler;
@Autowired
public OfferController(final OfferService offerService, OfferAssembler offerAssembler) {
this.offerService= checkNotNull(offerService);
this.offerAssembler= checkNotNull(offerAssembler);
}
@RequestMapping(value = "/search/findById", method = RequestMethod.GET, produces = { MediaType.APPLICATION_JSON_VALUE })
public ResponseEntity<PagedResources<OfferResource>> findOfferById(
@RequestParam(value = "offerId") long offerId, Pageable pageable,
PagedResourcesAssembler<OfferDetails> pagedResourcesAssembler) {
Page<OfferDetails> page = service.findById(offerId, pageable);
Link link = linkTo(methodOn(OfferController.class).findOfferById(offerId,
pageable,
pagedResourcesAssembler)).withSelfRel();
PagedResources<OfferResource> resource = pagedResourcesAssembler.toResource(page, assembler, link);
return new ResponseEntity<>(resource, HttpStatus.OK);
}
}
As a result, you will receive:
http://[your host]/[your app context]/offer/search/findById?offerId=[some offer id]{&page,size,sort}
source to share
The following solution builds on the answer provided by @palisade, but solves the issue of pagination options not showing in its own link - an issue noted by two of the comment comments that I encountered myself.
Replacing the palisade reference declaration ...
Link link = linkTo(methodOn(OfferController.class).findOfferById(offerId,
pageable,
pagedResourcesAssembler)).withSelfRel();
... with the next ...
Link link = new Link(ServletUriComponentsBuilder.fromCurrentRequest().build()
.toUriString())
.withSelfRel();
... I get links to pages that look like this:
{
"links": [
{
"rel": "first",
"href": "http://[your host]/[your app context]/offer/search/findById?offerId=[some offer id]&page=0&size=1"
},
{
"rel": "prev",
"href": "http://[your host]/[your app context]/offer/search/findById?offerId=[some offer id]&page=2&size=1"
},
{
"rel": "self",
"href": "http://[your host]/[your app context]/offer/search/findById?offerId=[some offer id]&page=3&size=1"
},
{
"rel": "next",
"href": "http://[your host]/[your app context]/offer/search/findById?offerId=[some offer id]&page=4&size=1"
},
{
"rel": "last",
"href": "http://[your host]/[your app context]/offer/search/findById?offerId=[some offer id]&page=6&size=1"
}
],
"content": [
{
...
source to share