title: Practical advice on using Jersey and Guice for RESTful service
body: <p>From what I can find online, the state of the art for Guice + Jersey integration has stagnated since 2008 when it appears both teams reached an impasse. The crux of the issue is that JAX-RS annotations perform field and method injection and this doesn't play nicely with Guice's own dependency injection.</p>
  
  <p>A few examples which I've found don't go far enough to elucidate:</p>
  
  <ul>
  <li><p>Iqbalyusuf's post on <a href="http://blog.iparissa.com/jersey-guice-on-google-app-engine-java/" rel="noreferrer">Jersey + Guice on Google App Engine Java</a> suffers from a lot of boilerplate (manually getting and calling the injector). I want binding and injection should happen behind the scenes via Guice annotations.</p></li>
  <li><p>Jonathan Curran's article <a href="http://blog.curran.in/2010/02/creating-restful-service-with-jersey.html" rel="noreferrer">Creating a RESTful service with Jersey, Guice, and JSR-250</a> gave me hope because it's much more current (2010), but went no further than showing how to start up a Jersey service inside of a Guice ServletModule. However, there are no examples of doing any real dependency injection. I suppose that was left as an exercise for the reader. Curran's post may in fact be the correct first step towards wiring up Guice and Jersey and so I plan on starting with that.</p></li>
  <li><p>tantalizingly <a href="http://macstrac.blogspot.com/2009/01/jax-rs-as-one-web-framework-to-rule.html" rel="noreferrer">James Strachan writes</a>:</p>
  
  <blockquote>
    <p>JAX-RS works well with dependency
    injection frameworks such as Spring,
    Guice, GuiceyFruit or JBossMC - you
    can basically pick whichever one you
    prefer.</p>
  </blockquote>
  
  <p>But I see no evidence that is true from a practitioner's point of view.</p></li>
  </ul>
  
  <p>What I find lacking are practical examples and explanations on how to combine JAX-RS and Guice annotations. For instance:</p>
  
  <ul>
  <li>I believe I cannot use constructor injection with any resource as Jersey wants to control this</li>
  <li>I'm uncertain whether I can combine @Inject with @PathParam, @QueryParam, et al.</li>
  <li>How to use injection in a MessageBodyWriter implementation</li>
  </ul>
  
  <p>Does anyone have examples, preferably with source, of non-trivial application which combines Jersey and Guice without sacrificing one or the other in the process? I'm keeping on this road regardless, but the bits and pieces on the Jersey and Guice lists makes me think I'm repeating the work of others who came before me.</p>