I'm using spring-data-common's PagedResourcesAssembler in my REST controller, and I was happy to see it even generates next/previous links in the response. However, in those cases where I have additional query parameters (besides page, size, sort), these are not included in the generated links. Can I somehow configure the assembler to include the parameters in the links?
Many thanks, Daniel
You need to build base link by yourself and pass it to "toResource" method of 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 get:
http://[your host]/[your app context]/offer/search/findById?offerId=[some offer id]{&page,size,sort}
The following solution is based on the answer provided by @palisade, but addresses the problem of pagination parameters not appearing in the self link--a problem noted by two of the answer's commenters, and which I experienced myself.
By replacing palisade's link declaration...
Link link = linkTo(methodOn(OfferController.class).findOfferById(offerId,
pageable,
pagedResourcesAssembler)).withSelfRel();
...with the following...
Link link = new Link(ServletUriComponentsBuilder.fromCurrentRequest().build()
.toUriString())
.withSelfRel();
...I'm getting page links 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": [
{
...
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