JAX-RS 2.0 Support for HalBuilder

Mark Derricutt <[email protected]> Fri, 28 Jun 2013 21:38:18 +1200
Newsgroups gmane.comp.web.services.rest
Message-ID <[email protected]>
'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.

Mark