users@jersey.java.net

[Jersey] Re: Basic JAX-RS 2.0 Support for HalBuilder

From: Jakub Podlesak <jakub.podlesak_at_oracle.com>
Date: Fri, 28 Jun 2013 15:14:48 +0200

Hi Mark,

On Jun 28, 2013, at 1:44 PM, Mark Derricutt <mark_at_talios.com> wrote:

> '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;
> }
>

Additionally, you may want to look at the Jersey Model Processors feature (see [1], [2]), that should allow you to enrich application
resources even without touching the source code directly. The HAL properties could be then bound globally.

> 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?

Look at [3], i.e. just specify required classes explicitly when creating your resource config.

HTH,

~Jakub

>
>
> Mark

[1]https://jersey.java.net/documentation/latest/resource-builder.html#d0e7790
[2]https://jersey.java.net/apidocs/latest/jersey/org/glassfish/jersey/server/model/Resource.Builder.html
[3]https://jersey.java.net/apidocs/latest/jersey/org/glassfish/jersey/server/ResourceConfig.html#ResourceConfig(java.lang.Class...)