I have a project that uses Jersey 1.7, Guice 3.0 and has some annotated JAXB classes that are serialized through resources for XML and JSON. I would like to set up JSON output using ContextResolver , as suggested in several questions here on SO, as well as in the Jersey User Guide . This includes creating a JSONJAXBContext as follows:
public class JaxbResolver implements ContextResolver<JAXBContext> { private final JAXBContext context; public JaxbResolver() throws Exception { this.ctx = new JSONJAXBContext( JSONConfiguration. natural(). humanReadableFormatting(true). build(), Resource1.class, Resource2.class); } }
My problem is that some of my resource classes have dependencies that Guice needs to introduce, for example:
public class DisplayConfigResource { private final ConfigRunner cr; @com.google.inject.Inject public DisplayConfigResource(ConfigRunner cr) { this.cr = cr; } }
If I remove my JaxbResolver from the game, everything will be fine, except that I have no control over the generated JSON (and by default it is really strange, for example, removing [] from singleton collections, ...). So, it seems common sense to hook up ContextResolver like mine in Jersey so that I can configure JSON for what I like. But
- the
JSONJAXBContext class really likes to have no-arg constructors on resources, and - My resources really would like their dependencies to be injected into their constructors.
So my question is how to resolve this situation, and are Jersey, Guise, and John playing well together?
source share