0

There are tons of examples on how to include Hypermedia links using JAXB and HATEOAS, but I cannot find any for Hypermedia + JSON. I like JAXB because just using annotations you can map the XML to the Object. Include HATEOAS and this is almost what I need, except its only in XML (because of JAXB of course). The problem is JAXB providers do not support JSON, as per 2.0 user guide.

So, using the example above, what I'm looking for is instead of this XML+Hypermedia output

<user-management>
  <users>/user-management/users</users>
  <report>/user-management/report</report>
</user-management>

Instead to get this JSON+Hypermedia output

{
  "_links": {
    "self": { "href": "/user-management" },
    "users": { "href": "/user-management/users" },
    "report": { "href": "/user-management/report" },
  }
}

*Yes, both examples are representations of the Resource, but examples of the Object/Model should be similar therefore I'm not including them.

Could it simply be that there's no framework to do this today since this JSON+Hypermedia specification has not be standardized? The most I found was a proposal, and a "To be continued..."

I am aware that I could created my own JAX-RS provider using MessageBodyWriter, but this only helps in marshalling "application/json" requests to the Object. I still need a way to get the Hypermedia links.

The reason I'm searching for this is because my current requirement is to return JSON data that includes Hypermedia links.

Does anyone have full working example? I cannot find any, and I vaguely recall seeing an article stating that I would need to "roll your own". Not desirable, but if that's what it will take...

Community
  • 1
  • 1
Jose Leon
  • 1,615
  • 3
  • 22
  • 30
  • There technically isn't a standard way to do it for XML, either. JAXB may be an approach, but what's important is whether the media type itself has the ability to convey links. Neither XML nor JSON can do that. However, a format like HAL sits "on top" of both and affords the media type the ability to express links. – Jonathan W Aug 30 '14 at 19:00

1 Answers1

0

You can use Jackson for this and it should be able to do what you want, i.e.:

@Data
public class Container {
    @JsonProperty("_links")
    Map<String,Link> links = new LinkedHashMap<>();
}

@Data
public class Link {
    @JsonProperty("href")
    private String href;
}

More formally, you could define a custom container rather than the generic one I defined.

Jonathan W
  • 3,759
  • 19
  • 20