Java 用于void下载方法的Spring HATEOAS模板链接
我的控制器中有一个方法,如下所示:Java 用于void下载方法的Spring HATEOAS模板链接,java,spring,spring-hateoas,Java,Spring,Spring Hateoas,我的控制器中有一个方法,如下所示: @RequestMapping(value = "/download/attachment/{attachmentId}", method = RequestMethod.GET) public void download(@PathVariable("attachmentId") String attachmentId, HttpServletRequest request, HttpServletResponse response)
@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)中使用以下代码来实现这一点:
我从中得到的链接不是模板。它是URL编码的。“{”以%7B的形式发送。我不希望发生这种情况。有人能提出任何建议吗?好的,因此我使用以下方法解决了此问题:
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");
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");