Implement HATEOAS with HAL in Jersey
Asked Answered
I

3

23

One of the key points of building a RESTful API is HATEOAS. Now, Jersey offers a linking ability which is quite good (see this link). But I have seen the draft of the HAL Specification and it seems to be a well thought piece of work.

I am interested if there is some lib that makes it easy to adhere to HAL in Jersey. I have seen the references mentioned in the draft like https://github.com/HalBuilder. But I am using direct POJO marshalling and I do not know how to mix that with Halbuilder.

So, is there already some lib that incorporates HAL into Jersey? Or maybe I can use some kind of filter to enhance the generated POJOs manually? If yes, can someone give me a clue where to look next to accomplish this?

Ingaborg answered 17/9, 2012 at 13:27 Comment(5)
Oh god, yet another spec by someone who thinks that namespaces are a bad thing and who seems to be only partially aware of existing work in this area. (Don't mind me. It's not your fault. Just depressing that so many people never ever learn. Sigh…)Debar
No problem, maybe you can hint me at the existing work? I am new to REST and trying to find my way around it.Choreograph
@DonalFellows What other work? Do you mean another standard for Hypermedia JSON? Or that you should just use XML for Hateoas? Between GitHUB, Facebook, Google, etc. each RESTful API seems to use a slightly different format for representing discovery links because no standard exists for JSON. What's wrong with using HAL or some such as a standard and then having a library like Halbuilder to make it simple to implement?Kirkcudbright
@Sponiro why can't you do POJO marshaling with HalBuilder? Wouldn't your client get a simple Representation that would have a bean in it that would be your POJO? This would live with the relations in the Representation object.Kirkcudbright
JSON-LD is better I think. But I guess I am a bit out of date :PLashaunda
L
2

I can't find any direct support for this in Jersey, but there should be ways to customize the marshaller that Jersey uses to convert your POJO's to JSON. (either by using HalBuilder, or create some HAL-compatible code of your own)

If you use Jerseys JAXB based JSON support, take a look at BootstrapTypeConverterTest in the jettison source code which demonstrates how a converter can be used to cusomize JSON output. Unit-testing conversions to/from JSON should be a simple task and jettison-project has good examples to look at.

If you use Jerseys "POJO Support"-method, take a look at JacksonHowToCustomSerializers for a similar example on how to create your custom serializers.

(If JAXB is involved, you can also do cool things like processing custom annotations.)

You probably have a pretty good idea of how your objects (input) looks like and what JSON code (output) you expect, so creating unit tests for these conversions should be a relatively simple task.

Lasalle answered 11/7, 2014 at 6:56 Comment(2)
halbuilder-jaxrs offers a nice integration. Do not forget to register JaxRsHalBuilderSupport as a message body writer.Ectogenous
I am going with github.com/Nykredit/jackson-dataformat-hal, looks lightweight and flexible.Fuhrman
M
1

Even if this is not really an answer, I had not enough characters to put it in a comment :)

If you're really new to REST, I think you're beginning probably in the wrong way. I don't know exactly your requirements, but if you only want to learn, begin with an easy REST service, use Web API from .net or something similar in Java (a simple servlet would be enough) and try to implement a service which only uses the tools delivered by http (the verbs, the request header and payload, the response message). REST API design rulebook from Marc Massé could be a good reference to learn. Create your own communication protocol with POJOs or POCOs and once you really what you can do REST and how it works, you can use a framework to ease your work.

If you begin with a framework you may miss the essence of the concept...

I hope it helps!

Melda answered 20/2, 2014 at 9:15 Comment(1)
I can only second that. It teaches a lot on many levels, without getting into framework hell. The most important thing one would learn is how to tell a good framework from a bad one.Percolator
K
0

It's very little that's required to adhere to HAL. I suggest you roll your own mini-library where you wrap your POJOs in HAL-objects. This way you get the code the way you like it and it will be way simpler than HalBuilder.

Karakoram answered 6/5, 2014 at 21:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.