Reputation: 310
I have a method in my controller as below :
@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();
}
I want to generate a templated HATEOAS link to this method using the ControllerLinkBuilder class that Spring provides. My link should look like :
"download" : {
"href" : "https://localhost:8080/download/attachment/{attachmentId}"
}
I am using the following code to do that in my ResourceAssembler class (which extends ResourceAssemblerSupport) :
Link downloadAttachmentLink = linkTo(MyRestController.class, MyRestController.class
.getMethod("download", String.class, HttpServletRequest.class, HttpServletResponse.class),
"{attachmentId}").withRel("download");
The link I am getting out of this is not templated. It is URL encoded. The "{" is sent as %7B. I don't want that to happen. Can anyone suggest anything?
Upvotes: 1
Views: 763
Reputation: 310
Ok, so I solved this problem using the following :
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");
Upvotes: 1