I'm trying to figure out how to build HAL links with templated: true
. If I use
BasicLinkBuilder.linkToCurrentMapping().slash("api/public/blogs/{blog}").withRel("blog");
The {
and }
chars are still encoded. Any idea how to build template URL links with Spring-hateo as 0.10.0.RELEASE
by its API?
Thanks.
HAL is a simple format that gives a consistent and easy way to hyperlink between resources in your API. The HAL format is strictly coupled to HATEOAS. The main target of HATEOAS is to decouple the API Consumer from the paths used in the API.
To implement HATEOAS, we would need to include related resources in the response. Instead of Student we use a return type of EntityModel<Student> . EntityModel is a simple class wrapping a domain object and allows adding links to it. We create a new resource.
Spring HATEOAS provides common abstractions (representational models, a Link class, API to build links pointing to Spring MVC controllers, etc.) to ease building hypermedia driven REST APIs with Spring MVC in general. Thus, you can use it alongside Spring MVC to manually build those services.
I'm also wondering how this is meant to be done using the HATEOAS API. For now we've worked around it by generating the Link objects using the BasicLinkBuilder and ControllerLinkBuilder classes, and then appending templated query params into a new Link(String href)
constructor. Interestingly, this builds a Link with a templated: true
attribute.
We noticed that attempting to pass in values such as {blog}
into the LinkBuilder classes ended in these values attempting to be replaced from values on the current request (i.e. the linkbuilder was attempting to find ?blog=value
from the current request and replace value
into the Link being built, and as this didn't exist was causing an exception.
Although the workaround isn't particularly nice my team hasn't been able to find any way of getting templated params into the LinkBuilders via the API without causing problems.
To get brackets in links I've ended up with a bit hacky solution, but as a temporal workaround works:
public class BracketsLink extends Link {
public BracketsLink(Link link) {
super(link.getHref().replaceAll("%7B", "{").replaceAll("%7D", "}"), link.getRel());
}
}
BracketsLink
class:new BracketsLink(linkTo(methodOn(MessageController.class).message("{id}")).withRel("message"))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With