【问题标题】:Generate links with additional query parameters using PagedResourcesAssembler使用 PagedResourcesAssembler 生成带有附加查询参数的链接
【发布时间】:2014-09-03 19:02:38
【问题描述】:

我在我的 REST 控制器中使用 spring-data-common 的 PagedResourcesAssembler,我很高兴看到它甚至在响应中生成下一个/上一个链接。但是,在我有其他查询参数(除了页面、大小、排序之外)的情况下,这些不包含在生成的链接中。我可以以某种方式配置汇编程序以在链接中包含参数吗?

非常感谢, 丹尼尔

【问题讨论】:

    标签: spring-hateoas spring-data-commons


    【解决方案1】:

    你需要自己建立基础链接,并将其传递给PagedResourcesAssembler的“toResource”方法。

    @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);
        }
    }
    

    结果你会得到:

    http://[your host]/[your app context]/offer/search/findById?offerId=[some offer id]{&page,size,sort}
    

    【讨论】:

    • 我尝试使用与您在答案中使用的相同类型的链接,但它对我不起作用。这是有效的:Link link = new Link(entityLinks.linkFor(Inventory.class, "name","page","size","sort").toString() + "/search/categoryName{?name,page,size,sort}").withSelfRel()。我打算再试一次,看看能不能让它像你的那样工作,但现在我很高兴它正在工作。
    • 非常感谢,但有几点。 1)您实际上不需要 .toResource 调用中的 'assembler' 参数,您只需使用页面和链接即可。 2)由于某种原因,自我链接似乎不包括分页参数,这很可惜(@HatemJaber 指出了这一点,我相信)。
    【解决方案2】:

    以下解决方案基于@palisade 提供的答案,但解决了分页参数未出现在自我链接中的问题——这个问题由答案的两位评论者指出,我自己也经历过。

    通过替换 Palisade 的链接声明...

    Link link = linkTo(methodOn(OfferController.class).findOfferById(offerId,
                    pageable,
                    pagedResourcesAssembler)).withSelfRel();
    

    ...与以下...

    Link link = new Link(ServletUriComponentsBuilder.fromCurrentRequest().build()
                   .toUriString())
                   .withSelfRel();
    

    ...我得到的页面链接如下所示:

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

    【讨论】:

      猜你喜欢
      • 2018-01-15
      • 2012-04-05
      • 1970-01-01
      • 1970-01-01
      • 2013-01-20
      • 2020-09-16
      • 2015-05-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多