I'm using Spring Boot and Spring Hateoas configured with @EnableHypermediaSupport(type = HAL). While this works fine in the basic scenario I'd like the to be able to add additional attributes to the links. For example it's easy to return links that'll render links such as this:
{
"_links":{
"self":{
"href":"http://localhost/"
},
"something":[
{
"href":"http://some-url.com/something1"
},
{
"href":"http://some-url.com/something2"
}
]
}
What I want to do is to add more attributes to the objects in the something rel. For example:
{
"_links":{
"self":{
"href":"http://localhost/"
},
"something":[
{
"name":"something1",
"href":"http://some-url.com/something1"
},
{
"name":"something2",
"href":"http://some-url.com/something2"
}
]
}
}
What's the best way to do this (preferably using the ControllerLinkBuilder) without creating my own DTO's? I've tried creating my own subclass of Link and add field for name (and getters and setters) but they seem to be ignored.
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.
Spring HATEOAS provides some APIs to ease creating REST representations that follow the HATEOAS principle when working with Spring and especially Spring MVC. The core problem it tries to address is link creation and representation assembly.
The Spring HATEOAS project is a library of APIs that we can use to easily create REST representations that follow the principle of HATEOAS (Hypertext as the Engine of Application State).
HAL support will get a significant upgrade, so I would wait.
I don't know how you use your subclass, but basically that approach works. You must not forget the annotation on your name
field. Example:
public SuperLink extends Link {
@XmlAttribute
private String name;
public SuperLink(Link link, String name) {
super(link.getHref(), link.getRel());
this.name = name;
}
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