5

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.

Aleksander Blomskøld
  • 18,374
  • 9
  • 76
  • 82
Johan
  • 37,479
  • 32
  • 149
  • 237

1 Answers1

5

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;
  }
a better oliver
  • 26,330
  • 2
  • 58
  • 66
  • I had left out the @XmlAttribute annotation but even though I add it to the field (just like you imply) the field won't be rendered. What version of Spring Hateoas are you using? – Johan Jan 16 '14 at 06:09
  • 1
    One problem was that I accidentally had both faster-jackson and jackson 1 in classpath. When I removed jackson one I got the following error: Caused by: java.lang.IllegalStateException: Conflicting property name definitions: 'links' (for [field org.springframework.hateoas.ResourceSupport#links]) vs '_links' (for [method org.springframework.hateoas.ResourceSupport#getLinks(0 params)]) But if I upgrade to 0.9.0.BUILD-SNAPSHOT everything seem to work. – Johan Jan 16 '14 at 07:04
  • @abetteroliver How to make this work? Do we have any working example? I am facing the similar issue...I have created the "SuperLink" but don't know where to add. Your help should be appreciable. – VelNaga Sep 11 '18 at 19:42
  • @VelNaga instead of `resource.add(new Link(...))` you use `resource.addLink(new SuperLink(...))`. If you need more information it's better to ask a new question and describe your situation. – a better oliver Sep 12 '18 at 19:09
  • @abetteroliver Already i have raised the question Please find it here https://stackoverflow.com/questions/52278347/spring-boot-customise-hypermediahateoas-response-and-add-additional-attribute could you please answer here...It would be really helpful for me – VelNaga Sep 12 '18 at 19:13
  • @abetteroliver I did a try but it is not working....your help should be really really appreciable – VelNaga Sep 12 '18 at 20:12
  • How to write TestTemplate for this custom link? Because RestTemplate now aware of this custom link object.It would be breat if you answer this question if you know the answer..https://stackoverflow.com/questions/54427139/how-to-bind-custom-hateoas-link-class-to-resttemplate – VelNaga Jan 30 '19 at 05:46