Tuesday, November 3, 2009

JAX-RS, Jersey and Groovy

Groovy makes writing multi-threaded daemon a snap. While searching for a way to control such daemon simply, a co-worker suggested I use JAX-RS. More specifically, he recommended Jersey, sun's reference implementation of JAX-RS or JSR 311.

Jersey made it extremely easy to implement a RESTful web service inside my Groovy based daemon in order to control it while running. Groovy's dynamic class loading nature did case some issues with the resource file. In the end it was easy to overcome though.To get up an running quickly follow the getting started guide. You can create a groovy class which uses the Resource Class annotations they show. Then inside your main application you can spawn the webservice like so:


def resourceConfig = new DefaultResourceConfig( JSONRPCService.class )
HttpServer myServer = HttpServerFactory.create("http://localhost:9090/", resourceConfig );
myServer.start();


Notice that we have to create resource config object and pass it to the HttpServerFactory. Thats the key, since under groovy the server factory can not seem to find the resource on its own.

That's it. Next post I will how how to create an JSON-RPC interface using Jersey and JSON-lib.

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home