【问题标题】:Spring HATEOAS templated link for void download method用于无效下载方法的 Spring HATEOAS 模板链接
【发布时间】:2018-04-14 07:29:00
【问题描述】:

我的控制器中有一个方法如下:

    @RequestMapping(value = "/download/attachment/{attachmentId}", method = RequestMethod.GET)
public void download(@PathVariable("attachmentId") String attachmentId, HttpServletRequest request,
        HttpServletResponse response) throws IOException {
    InputStream file = myCustomObject.getAttachmentById(attachmentId);
    response.setContentType("application/octet-stream");
    response.flushBuffer();
}

我想使用 Spring 提供的 ControllerLinkBuilder 类生成指向此方法的模板化 HATEOAS 链接。我的链接应该是这样的:

"download" : {
          "href" : "https://localhost:8080/download/attachment/{attachmentId}"
}

我在我的 ResourceAssembler 类(扩展 ResourceAssemblerSupport)中使用以下代码:

            Link downloadAttachmentLink = linkTo(MyRestController.class, MyRestController.class
                .getMethod("download", String.class, HttpServletRequest.class, HttpServletResponse.class),
                "{attachmentId}").withRel("download");

我从中得到的链接不是模板化的。它是 URL 编码的。 “{”作为 %7B 发送。我不希望这种情况发生。任何人都可以提出任何建议吗?

【问题讨论】:

    标签: java spring spring-hateoas


    【解决方案1】:

    好的,所以我使用以下方法解决了这个问题:

                Link downloadAttachmentByIdLink = new Link(
                    new UriTemplate(
                            linkTo(MyRestController.class,
                                    MyRestController.class.getMethod("download", String.class,
                                            HttpServletRequest.class, HttpServletResponse.class),
                                    "").toUriComponentsBuilder().build().toUriString(),
                            new TemplateVariables(
                                    new TemplateVariable("attachmentId", TemplateVariable.VariableType.SEGMENT))),
                    "download");
    

    【讨论】:

      猜你喜欢
      • 2023-03-28
      • 1970-01-01
      • 1970-01-01
      • 2013-07-21
      • 2016-04-14
      • 1970-01-01
      • 2016-05-23
      • 2017-12-10
      • 2014-11-06
      相关资源
      最近更新 更多