users@jersey.java.net

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

From: Ted M. Young [_at_jitterted] <tedyoung_at_gmail.com>
Date: Wed, 8 Jan 2014 21:43:11 -0800

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
>