users@jersey.java.net

[Jersey] Basic JAX-RS 2.0 Support for HalBuilder

From: Mark Derricutt <mark_at_talios.com>
Date: Fri, 28 Jun 2013 23:44:41 +1200

'lo all,

Just added a super simple JAX-RS 2.0 support module for my HalBuilder
JVM API which I intend to release sometime over the weekend:

https://github.com/HalBuilder/halbuilder-jaxrs

along with a small sample application at:

https://github.com/HalBuilder/halbuilder-examples/tree/develop/halbuilder-jersey-example


Basically, when configuring Jersey ( I've not looked at RestEasy yet )
you simple tell it to also scan the
"com.theoryinpractise.halbuilder.jaxrs" package to pick up the support
classes, then in your resource class just return your representation
object:

     @GET
     @Produces({RepresentationFactory.HAL_JSON,
RepresentationFactory.HAL_XML})
     public Representation getIt() {
         Representation rep = representationFactory.newRepresentation();
         rep.withProperty("message", "Got it!");
         return rep;
     }

and voila - content neg. is all handled by JAX-RS out of the box:

$ curl -H "Accept: application/hal+xml"
http://localhost:8080/myapp/myresource
<resource>
<link rel="website" href="http://gotohal.net" />
<message>Got it!</message>
</resource>

$ curl http://localhost:8080/myapp/myresource
{
   "_links" : {
     "website" : {
       "href" : "http://gotohal.net"
     }
   },
   "message" : "Got it!"
}

So far I've added the @Produces side of things, tho @Consumes should be
just as simple and should be added before the release.

Note that the support ONLY works for returning
Representation/ReadableRepresentation and not arbitrary objects - this
is me trying to make you think of links/representations etc. as
first-class things.

One thing I don't like is having to use the package name in a String for
scanning, is there a way I can provide a class that specifies the
classes concretely?


Mark