users@jersey.java.net

[Jersey] Re: How Do I Provide a Custom ObjectMapper?

From: Michael Iles <michael.iles_at_gmail.com>
Date: Thu, 9 Jan 2014 16:13:29 -0500

Your solution helps but it's for Jackson 1.x and not for Jackson
2.3.1. (I'm using Jackson 2 because another component depends on it.)

This SO question is asking the same thing [1] but the answer [2]
exhibits the same problem that I'm running into, which is that Jersey
ends up with TWO Jackson providers registered.

So I'm still in the same situation: I either need to get access to the
Jackson provider that Jersey auto-discovers (so that I can configure
its ObjectMapper), or I need to disable the auto-discovery so I can
explicitly register my own provider.

Please help!

Mike

[1] http://stackoverflow.com/questions/18872931/custom-objectmapper-with-jersey-2-2-and-jackson-2-1
[2] http://stackoverflow.com/a/18917918/910062

On 9 January 2014 00:43, Ted M. Young [@jitterted] <tedyoung_at_gmail.com> wrote:
> Yeah, the docs around this make it hard to find the right solution. Take a
> look at https://jersey.java.net/documentation/latest/media.html#d0e6652 and
> see if that works for you. You'll basically be writing a @Provider that will
> create an instance of an ObjectMapper that'll be injected into Jersey.
>
> ;ted
> --
> http://about.me/tedmyoung
>
>
>
>
> On Wed, Jan 8, 2014 at 9:30 PM, Michael Iles <michael.iles_at_gmail.com> wrote:
>>
>> I need to provide a custom Jackson ObjectMapper to Jersey so that I
>> can register the Joda Time module with it and configure other things
>> like FAIL_ON_EMPTY_BEANS, etc.
>>
>> In Jersey 1 I gave a @Provider which implemented
>> ContextResolver<ObjectMapper> to give my custom OjbectMapper.
>>
>> In Jersey 2 I bring in the jackson-jaxrs-json-provider artifact which
>> auto-registers itself with Jersey. However I want to *manually*
>> register it, so that I can customize it:
>>
>> public class MyApplication extends ResourceConfig {
>>
>> public MyApplication() {
>> JacksonJaxbJsonProvider provider = new
>> JacksonJaxbJsonProvider();
>> provider.setMapper(Marshaller.instance.objectMapper);
>> register(provider);
>> }
>> }
>>
>> My problem is now that I have *two* Jackson providers registered with
>> Jersey:
>>
>> 2014-01-08 23:50:38,823 [main]
>> org.glassfish.jersey.server.ApplicationHandler:558 INFO Jersey
>> application initialized.
>> Root Resource Classes:
>> ...
>> Message Body Readers:
>> com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider
>> com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider
>> ...
>> Message Body Writers:
>> com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider
>> com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider
>> ...
>>
>> What's the right way to do this? I either want to get access to the
>> Jackson provider that Jersey auto-discovered (so that I can change its
>> ObjectMapper) or I want to stop the auto-discovery so that I can
>> register my own Jackson provider.
>>
>> Mike
>
>