How to build HAL links with "templated: true" using Spring -hateoas?

I am trying to figure out how to create HAL links with templated: true . If i use

 BasicLinkBuilder.linkToCurrentMapping().slash("api/public/blogs/{blog}").withRel("blog"); 

The characters { and } are still encoded. Any idea on how to create a template url with Spring -hateo as 0.10.0.RELEASE by its API?

Thanks.

+6
source share
2 answers

I am also wondering how this should be done using the HATEOAS API. Currently, we have been working on this by creating Link objects using the BasicLinkBuilder and ControllerLinkBuilder classes, and then adding the template request parameters to the new Link(String href) constructor. Interestingly, this creates a link with the templated: true attribute.

We noticed that an attempt to transfer values, such as {blog} to LinkBuilder classes, ended up replacing these values ​​with the values ​​of the current request (ie linkbuilder tried to find ?blog=value Blog ?blog=value from the current request and replace value with the constructed channel and since this was not, an exception is thrown.

Although the workaround is not very pleasant, my team was unable to find a way to get the template parameters in LinkBuilders via the API without causing problems.

+3
source

To get the brackets in the links, I got a little hacky solution, but how a temporary workaround works:

  • create class:
 public class BracketsLink extends Link { public BracketsLink(Link link) { super(link.getHref().replaceAll("%7B", "{").replaceAll("%7D", "}"), link.getRel()); } } 
  • and create links using the BracketsLink class:
 new BracketsLink(linkTo(methodOn(MessageController.class).message("{id}")).withRel("message")) 
+1
source

All Articles