我的控制器中有一个方法如下:
\2
我想使用Spring提供的ControllerLinkBuilder类为此方法生成模板化的HATEOAS链接。我的链接应该如下:
@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();
}
我使用以下代码在我的ResourceAssembler类(扩展ResourceAssemblerSupport)中执行此操作:
"download" : {
"href" : "https://localhost:8080/download/attachment/{attachmentId}"
}
我从中获得的链接不是模板化的。它是URL编码的。 “{”以%7B的形式发送。我不希望这种情况发生。任何人都可以提出任何建议吗?
答案 0 :(得分: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");