Spring Boot (HATEOAS) ControllerLinkBuilder.linkTo(...) 查询参数丢失/未创建 [英] Spring Boot (HATEOAS) ControllerLinkBuilder.linkTo(...) query parameter missing/not created

查看:45
本文介绍了Spring Boot (HATEOAS) ControllerLinkBuilder.linkTo(...) 查询参数丢失/未创建的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Spring Boot (1.4) 开发 REST API,并且还使用 PagedResourcesAssembler 来创建带有分页的 JSON 响应,这很有魅力.来自 Spring 的家伙们做得很好!但是,我在提供 URL 查询参数(如果提供)时遇到了问题.我知道 PagedResourcesAssembler 无法自己找出 URL 查询参数,所以我想为它提供一个链接,使用 ControllerLinkBuilder.linkTo(controller, parameters) 方法:

I'm working on a REST API with Spring Boot (1.4) and also using the PagedResourcesAssembler to create JSON responses with pagination, which works like a charm. The guys from Spring did a great job! However I'm having issues providing the URL query parameters (if provided). I understand that the PagedResourcesAssembler can't figure out URL query parameter on its own, so I wanted to provide a Link for it, using the ControllerLinkBuilder.linkTo(controller, parameters) method:

@RequestMapping(produces = MediaType.APPLICATION_JSON_VALUE)
public HttpEntity<PagedResources<Document>> find(final DocumentSearch searchForm, final Pageable pageable, final PagedResourcesAssembler assembler) {

    final Page<Document> documents = documentService.find(searchForm, pageable);

    // this map is just for this example ..
    final Map<String, Object> parameters = new HashMap<>();
    parameters.put("text", "foobar");

    final Link link = ControllerLinkBuilder.linkTo(DocumentController.class, parameters).withSelfRel();

    return ResponseEntity.ok(assembler.toResource(documents, link));
}

如您所见,我为链接明确提供了一个参数,但相应的响应中没有 URL 查询:

as you can see I distinctly provided a parameter for the link, but the corresponding response has no URL query in it:

    {
    "_links": {
        "first": {
            "href": "http://localhost:8080/documents?page=0&size=20"
        },
        "self": {
            "href": "http://localhost:8080/documents"
        },
        "next": {
            "href": "http://localhost:8080/documents?page=1&size=20"
        },
        "last": {
            "href": "http://localhost:8080/documents?page=2&size=20"
        }
    }

我也尝试调试它,他正在调用:

I also tried to debug into it, he's calling:

linkTo:123, ControllerLinkBuilder (org.springframework.hateoas.mvc)
expand:152, UriComponents (org.springframework.web.util)
expandInternal:47, HierarchicalUriComponents (org.springframework.web.util)
expandInternal:330, HierarchicalUriComponents (org.springframework.web.util)

expandInternal:340, HierarchicalUriComponents (org.springframework.web.util)直到此时他仍然拥有我的 text=foobar,同时作为 QueryUriTemplateVariable但随后在

expandInternal:341, HierarchicalUriComponents (org.springframework.web.util)他不会进入他会/可以将我的 text=foobar 放入他的结果地图的 for 循环

linkTo:123, ControllerLinkBuilder (org.springframework.hateoas.mvc)
expand:152, UriComponents (org.springframework.web.util)
expandInternal:47, HierarchicalUriComponents (org.springframework.web.util)
expandInternal:330, HierarchicalUriComponents (org.springframework.web.util)

expandInternal:340, HierarchicalUriComponents (org.springframework.web.util) until this point he still has my text=foobar, meanwhile as QueryUriTemplateVariable but then at

expandInternal:341, HierarchicalUriComponents (org.springframework.web.util) he isn't going into the for-loop where he would/could put my text=foobar into his result map

非常感谢任何帮助.

最好的问候,彼得

更新

为了完成这项工作,我必须做两件事.首先,更改方法签名并添加带有相应参数的@RequestParam,我希望将其作为分页链接中的参数,然后使用 ControllerLinkBuilder 中的 methodOn:>

In order to make this work, I had to do two things. First, change the method signature and add @RequestParam with the corresponding parameter I'd like to have as parameter in pagination link and second, use the methodOn from ControllerLinkBuilder:

public HttpEntity<PagedResources<Document>> find(@RequestParam(value = "text", required = false) final String text, final Pageable pageable, final PagedResourcesAssembler assembler) {

final Link link = ControllerLinkBuilder.linkTo(
            ControllerLinkBuilder.methodOn(
                    DocumentController.class).find(text, pageable, assembler)).withSelfRel();

非常感谢你们的帮助,干杯.

Thank you both very much for your help, cheers.

附言有没有办法在没有 methodOn 的情况下完成这项工作?

P.s. is there any way to make this work without methodOn?

推荐答案

根据@RequestMapping 生成链接.您传递的参数仅用于替换指定 URL 中的变量.它们不是添加的查询参数.你需要自己做.

Links are generated according to the @RequestMapping. The parameters you pass are only to replace variables in the specified URL. They are not query parameters that are added. You need to do that yourself.

这篇关于Spring Boot (HATEOAS) ControllerLinkBuilder.linkTo(...) 查询参数丢失/未创建的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆