Question

I'm trying to enable JAXB annotation support within my RESTEasy based web application and this article was suggested to me (http://wiki.fasterxml.com/JacksonJAXBAnnotations). I'm able to get the jackson-xc.jar but I don't see how to register the annotation introspector. Currently, RESTEasy automatically serializes my JSON responses, where would the below fit? Currently, RESTeasy serializes the JSON object automatically.

Include jackson-xc jar, which contains org.codehaus.jackson.xc.JaxbAnnotationIntrospector Register this annotation introspector

    ObjectMapper mapper = new ObjectMapper();
    AnnotationIntrospector introspector = new JaxbAnnotationIntrospector();
    // make deserializer use JAXB annotations (only)
    mapper.getDeserializationConfig().setAnnotationIntrospector(introspector);
    // make serializer use JAXB annotations (only)
    mapper.getSerializationConfig().setAnnotationIntrospector(introspector);
Was it helpful?

Solution

What you need to figure out is how to make RESTeasy use ObjectMapper you have configured. JAX-RS offers generic way to do this via providers, but there may be simpler way(s) to do this with RESTeasy. Some JAX-RS implementations also register JAXB annotation introspector by default.

Last thing: are you sure you need to use JAXB annotations? While Jackson can use them, preferable method is to either just use basic naming convention for discovery and jackson's own annotations in case where overrides are needed. Problem with JAXB is that it is xml-specific so there is little bit of impedance when used with JSON.

OTHER TIPS

Thanks for the reply, I'll take a look at your suggestions...

The reason why we want to stick with the jaxb annotations is that the @XmlRootElement(name="userResponse") annotation allows an extra wrapping element that we want in our JSON responses. See example below:

We want to have {"response":{"userResponse":[{"user":{"businessRegion":"US","firstName":"joe","language":"en","lastName":"smith"}}}]} instead of what Jackson currently outputs, {"response":[{"user":{"businessRegion":"US","firstName":"joe","language":"en","lastName":"smith"}}]}. Is there anyway to mimic the @XmlRootElement in Jackson without adding an additional wrapper class?

User class: @XmlAccessorType(XmlAccessType.FIELD) @XmlRootElement(name="userResponse") public class UserResponse extends AbstractResponse{ private Users user;

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top