users@jersey.java.net

[Jersey] Re: JAXB and Jackson annotations together

From: Tatu Saloranta <tsaloranta_at_gmail.com>
Date: Thu, 5 Jul 2012 16:34:25 -0700

On Thu, Jul 5, 2012 at 11:29 AM, Brian Mulholland
<blmulholland_at_gmail.com> wrote:
>>> Brian said:
...
>> If you can reproduce the issue outside of Jersey with just Jackson
>> (configured to use both annotations), it would be easier to figured
>> out the problem.
>
> Unfortunately, the opposite appears to be true. Outside Jersey it
> works fine with the same annotations. I also changed my Jersey method

Ok.

> to accept and return a String, so that Jersey will not try to
> translate it, and then use Jackson manually inside the method. This
> worked flawlessly. So that gives me a viable workaround, though I
> really would prefer the cleanliness of letting jersey handle it.

Understandable.

>> One possible source of problems is that semantics of JAXB annotations
>> differs a bit from that of Jackson's native handling; especially with
>> Jackson versions prior to 1.9. This could cause problems when JAXB
>> annotations are only added on getter or setter, not both. But this is
>> just one possibility.
>
> I was using 1.8.3, but I upgraded to 1.9.8 and get the same behavior.
> I DO in fact have @XmlElement and @XmlAttribute only on get methods,
> when I tried to put them on both, JAXB broke, and it did not help
> Jackson. Same exact error. Note that when I use the Jersey CLIENT in
> my test methods, it works (IOW, it doesn't fail until it reaches the
> service, which then exceptions and returns error). The client looks
> like the following.
>
> ClientConfig config = new DefaultClientConfig();
> config.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING,
> Boolean.TRUE);
> config.getClasses().add(JacksonJsonProvider.class);
> Client client = Client.create(config);
> WebResource service = client.resource(getBaseURI());
> BeanParent parent = prepObj();
> ClientResponse response = service.path("marshal")
> .path("json")
> .accept(MediaType.APPLICATION_JSON)
> .type(MediaType.APPLICATION_JSON_TYPE)
> .post(ClientResponse.class, parent);
>
> In terms of server Jersey configuration, all I do is configure the
> pojomapping feature to true in web.xml. AFAIK, no other relevant
> configurations differ. I presume that the reason this works on the
> client is that I configured the JacksonJsonProvider explicitly? Is
> there a way I can do something equivalent in web.xml? I saw something
> similar online. People suggested adding org.codehaus.jackson.jaxrs to
> the packages to scan, but that made no difference, perhaps since the
> JAXB one is in the same package. Perhaps those posts are dated. Is
> there some other mechanism?

That sounds good to me; I don't use auto-scanning myself, too much magic.
It is possible to explicitly register ObjectMapper (by registering
provider for it), so that is one thing you could try to use. Or, by
manually constructing JacksonJaxbJsonProvider (which registers both
JAXB and Jackson annotation introspectors), adding provider for it.

-+ Tatu +-