Generate links with additional query parameters using PagedResourcesAssembler

Deadly 提交于 2019-12-22 06:56:02

问题


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


回答1:


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}



回答2:


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": [
        {
            ...


来源:https://stackoverflow.com/questions/25651820/generate-links-with-additional-query-parameters-using-pagedresourcesassembler

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!