users@jersey.java.net

[Jersey] Re: Proper Configuration of Jackson 2.0 in Jersey 2.6

From: Jakub Podlesak <jakub.podlesak_at_oracle.com>
Date: Tue, 25 Feb 2014 10:21:59 +0100

Hi all,

I think i need to clarify one thing. The reason, why Jackson provider
failed to resolve the ObjectMapper instance was in fact a bug in Jersey,
that prevented proper injection of providers registered via META-INF/services
mechanism (see https://java.net/jira/browse/JERSEY-2335, https://java.net/jira/browse/JERSEY-2322).

The bug was fixed in 2.6, so no matter how you register Jacskon 2.x providers
in an application built with Jersey 2.6+, things should work fine.

Detailed info bellow inline...

On 24 Feb 2014, at 20:59, tommif_at_iki.fi wrote:

> Hopefully I'm not talking too much bull here. Let's try:
>
> For future reference to any hapless soul trying to meddle with Jersey's
> twisted magic:
>
> The center ingredient to customizing Jackson's ObjectMapper is to make
> Jersey use Jackson's own JAX-RS <-> Jackson MessageBodyReader and
> MessageBodyWriter instead of it's own.
> - Jackson's JAX-RS integration looks up the ObjectMapper instance from
> ContextResolvers.

Correct, but not if you stick with Jersey 2.5 (and earlier) and register
the Jackson providers via META-INF/services mechanism (i.e. you
just introduce Jackson JAX-RS jar dependency in you project)

> - Jersey-provided Jackson integration doesn't, and instead instantiates
> it's own, leaving no room for customizing the ObjectMapper.

That is not correct, the only difference is Jersey’s Jackson feature
allows registered Jackson 1.x provider to be properly injected.
So that ObjectMapper customisation works just fine.
If you think otherwise, please send me a reproducible test case or file a bug.

>
> Step 1. Implement ContextResolver<ObjectMapper> to provide your
> customized ObjectMapper instance and register it with Jersey.

Right.

> Step 2. Register
> com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider with Jersey.

For Jersey 2.6+ this could be done simply by introducing the appropriate
dependency to Jacskon JAX-RS jar.

HTH,

~Jakub

> Step 3. Done.
>
>
> On 24.2.2014 19:16, Robert DiFalco wrote:
>> I'm not really clear on the correct path to do this in 2.6. I had to
>> go through a lot in 2.5x. Am I to understand that I know longer use
>> JacksonFeature? That I only need to declare a ContextResolver for my
>> ObjectMapper instance? If so this doesn't seem to be working for me.
>> Maybe I need to register the my ContextResolver explicitly (not just
>> annotated with Provider)? Or maybe I'm missing a dependency? I assumed
>> I could now exclude media jackson json.
>>
>> The Jersey documentation is not up to date on this matter and still
>> seems to be advising on the method used in 2.5 to configure Jackson 1.x.
>>
>> TIA
>