Question

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.

Was it helpful?

Solution

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;
  }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top