title: Using Jackson in Jersey with multiple configured ObjectMappers
body: <p>Is it possible to setup Jersey using Jackson for serialization/deserialization using multiple configured <code>ObjectMappers</code>?</p>
  
  <p>What I would like to be able to do is register a "default" Jackson <code>ObjectMapper</code> and then have the ability to register another feature which provides an <code>ObjectMapper</code> with some specialized configuration which under certain circumstance will "override" the "default" <code>ObjectMapper</code>.</p>
  
  <p>For example, this <code>ContextResolver</code> would be for the "default" mapper:</p>
  
  <pre><code>@Provider
  @Consumes(MediaType.APPLICATION_JSON)
  @Produces(MediaType.APPLICATION_JSON)
  public class JacksonMapperProvider implements ContextResolver&lt;ObjectMapper&gt; {
      private final ObjectMapper mObjectMapper;
  
      public JacksonMapperProvider() {
          mObjectMapper = createMapper();
      }
  
      protected abstract ObjectMapper createMapper() {
          ObjectMapper mapper = createMapper();
  
          return mapper
              .setSerializationInclusion(Include.ALWAYS)
              .configure(JsonParser.Feature.ALLOW_COMMENTS, true)
              .configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true)
              .configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true)
              .configure(JsonParser.Feature.ALLOW_UNQUOTED_CONTROL_CHARS, true);
      }
  
      @Override
      public ObjectMapper getContext(Class&lt;?&gt; type) {
          return mObjectMapper;
      }
  }
  </code></pre>
  
  <p>And this <code>ContextResolver</code> would be to override the "default" mapper:</p>
  
  <pre><code>@Provider
  @Consumes(MediaType.APPLICATION_JSON)
  @Produces(MediaType.APPLICATION_JSON)
  public class SpecializedMapperProvider implements ContextResolver&lt;ObjectMapper&gt; {
      private final ObjectMapper mObjectMapper;
  
      public SpecializedMapperProvider() {
          mObjectMapper = createMapper();
      }
  
      protected abstract ObjectMapper createMapper() {
          ObjectMapper mapper = createMapper();
  
          return mapper
              .disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
              .setDateFormat(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS"))
              .registerModule(new SpecializedModule1())
              .registerModule(new SpecializedModule2());
      }
  
      @Override
      public ObjectMapper getContext(Class&lt;?&gt; type) {
          if(SomeType.isAssignableFrom(type)) {
              return mObjectMapper;
          }
          return null;
      }
  }
  </code></pre>
  
  <p>I see in the <code>JacksonJsonProvider</code> code that Jackson supports <code>ObjectMapper</code> provider injection/resolution.  However, in practice, what I am seeing is that the "order" of the providers seems random (I'm guessing it's not, but I can't sort out how to control the order).  Sometimes the "override" comes before the "default" and everything works, but on the next server startup the order changes. </p>
  
  <p>I have attempted to get this to work in a number of ways including:</p>
  
  <ul>
  <li>Registering the <code>ContextResolver&lt;ObjectMapper&gt;</code> implementations manually (in differing orders)</li>
  <li>Registering the <code>ContextResolver&lt;ObjectMapper&gt;</code> implementations via <code>@Provider</code> annotations</li>
  <li>Specifying a priority when registering</li>
  </ul>
  
  <p>I am using the following:</p>
  
  <ul>
  <li>Jersey 2.8</li>
  <li>Jackson 2.3.3</li>
  </ul>
  
  <p>Perhaps I am taking a completely incorrect approach?<br>
  Is there a better way to achieve what I am trying to do? 
  Maybe I should just define two separate JAX-RS applications and have a single ObjectMapper configuration for each?</p>