title: Jersey and Guice and nice JSON
body: <p>I have a project which uses Jersey 1.7, Guice 3.0 and has some JAXB annotated classes which are serialized through resources to XML and JSON. I'd like to configure the JSON output using a <code>ContextResolver</code> as suggested in several questions here on SO, as well as in the <a href="http://jersey.java.net/nonav/documentation/latest/user-guide.html#d4e934" rel="nofollow">Jersey User Guide</a>. This involves creating a <code>JSONJAXBContext</code> like this:</p>
  
  <pre><code>public class JaxbResolver implements ContextResolver&lt;JAXBContext&gt; {
  
      private final JAXBContext context;
  
      public JaxbResolver() throws Exception {
          this.ctx = new JSONJAXBContext(
              JSONConfiguration.
                  natural().
                  humanReadableFormatting(true).
                  build(),
              Resource1.class, Resource2.class);
      }
  
      /* ... */
  }
  </code></pre>
  
  <p>My problem is, that some of my resource classes have dependencies which are to be injected by Guice, like this:</p>
  
  <pre><code>public class DisplayConfigResource {
      private final ConfigRunner cr;
  
      @com.google.inject.Inject
      public DisplayConfigResource(ConfigRunner cr) {
          this.cr = cr;
      }
  
      /* ... */
  }
  </code></pre>
  
  <p>If I remove my <code>JaxbResolver</code> from the game, everything works fine except that I have no control over the generated JSON (and the default is really weird, like removing the <code>[]</code>s from single-element collections, ...). So it seems it's common sense to plug a <code>ContextResolver</code> like mine into Jersey so I can tune the JSON to something I like. <em>But</em> </p>
  
  <ul>
  <li>the <code>JSONJAXBContext</code> class really likes to have no-arg constructors on the resources while</li>
  <li>my resources really like to have their dependecies injected in their constructors.</li>
  </ul>
  
  <p>So my question is how to resolve this situation and have Jersey, Guice and JSON play nicely together?</p>