Jersey and guice and nice json

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?

+4
source share
2 answers

You can also use Jackson instead of JAXB for Marshal / unmarshal JSON. It uses the same annotations @XmlRootElement , @XmlType etc., and it produces more standard output (and does not need these useful ContextResolver materials).

First configure your web.xml:

 <servlet> <servlet-name>jersey</servlet-name> <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class> <init-param> <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name> <param-value>true</param-value> </init-param> </servlet> 

Then add the jersey-json dependency to your pom.xml:

 <dependency> <groupId>com.sun.jersey</groupId> <artifactId>jersey-json</artifactId> <version>1.7</version> </dependency> 
+2
source

I had a similar problem and solved it by creating a custom JAXBContextResolver and manually specifying which classes would play well with json serialization:

 @Provider public class JAXBContextResolver implements ContextResolver<JAXBContext> { private JAXBContext context; private Class<?>[] types = {DtoIdNazov.class, DtoLokalitaPoloha.class, DtoListRestauracie.class, DtoDetailRestauracia.class}; public JAXBContextResolver() throws Exception { JSONConfiguration jsonConfiguration = JSONConfiguration.natural().build(); this.context = new JSONJAXBContext(jsonConfiguration, types); } public JAXBContext getContext(Class<?> objectType) { for (Class<?> type : types) { if (type == objectType) { return context; } } return null; } } 
0
source

All Articles