title: Where to register custom ObjectMapper for my REST Service application?
body: <p>I am building a REST service application with Spring and Jersey. </p>
  
  <p>The "Response Content Type" can be either JSON or XML, which is why I use JAXB annotations on the fields of my POJO, which works great so far. No configuration was needed to get this to work, I just put the required .jars on the classpath.</p>
  
  <p>Now, I want to add custom behaviour for (de-)serialization of Java 8 Date/Time objects and register the <code>JavaTimeModule</code> from Jackson.</p>
  
  <p>Where would be the right place to register the Module? I know the code snippet is supposed to look something like this:</p>
  
  <pre><code>ObjectMapper tmpMapper = new ObjectMapper();
  tmpMapper.registerModule(new JavaTimeModule());
  </code></pre>
  
  <p>But where do I actually put this code? </p>
  
  <p>The place that comes to mind is my <code>ResourceConfig</code> class from Jersey. It looks like this:</p>
  
  <pre><code>@ApplicationPath("/rest")
  public class ApplicationResourceConfig extends ResourceConfig {
      public ApplicationResourceConfig() {
      // Not needed because since jersey 2.9 JacksonFeature implements AutoDiscoverable
      // register(JacksonFeature.class);
  
      register(WadlFeature.class);
  
      // Registers our own request listener for monitoring purposes
      register(RestServiceApplicationEventListener.class);
  
      // Registers the package that contains our REST resources
      packages(PingResource.class.getPackage().getName());
  }
  </code></pre>
  
  <p>What I did now was to add the following lines at the bottom of the constructor:</p>
  
  <pre><code>ObjectMapper tmpMapper = new ObjectMapper();
  tmpMapper.registerModule(new JavaTimeModule());
  JacksonJaxbJsonProvider tmpProvider = new JacksonJaxbJsonProvider();
  tmpProvider.setMapper(tmpMapper);
  register(tmpProvider);
  </code></pre>
  
  <p>The DateTime conversion now worked. However, some of the the JAXB behaviour changed which caused the deserialization of other beans to break. Here is what is not working anymore:</p>
  
  <p>I have super class which defines a getter of a field:</p>
  
  <pre><code>public abstract class SuperClass {
     @XmlElement(name = "yourField")
     public abstract String getMyField();
  }
  </code></pre>
  
  <p>(This maps the internal name of the field to different name which is exposed to the outside)</p>
  
  <pre><code>public class SubClass {
      private String myField;
  
      @Override
      public String getMyField() {
          return myField;
      }
  }
  </code></pre>
  
  <p>This mechanism used to work before and I could e.g. define a POST parameter of type SubClass calling the method with a JSON snippet that looks like this:</p>
  
  <pre><code>{
      "yourField" : "hello world"
  }
  </code></pre>
  
  <p>When trying this now I get the exception:</p>
  
  <pre><code>com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "yourField" (class com.myProject.SubClass), not marked as ignorable (1 known property: "myField"])
  at [Source: org.glassfish.jersey.message.internal.ReaderInterceptorExecutor$UnCloseableInputStream@728b1889; line: 4, column: 14] (through reference chain: com.myProject.Subclass["yourField"])
  </code></pre>
  
  <p>Note however, that the basic JAXB features still work, only in this particular superclass/subclass example.</p>
  
  <p>Could it be that explicitly registering a <code>JacksonJaxbJsonProvider</code> somehow altered the default behaviour on how to deal with JAXB annotated beans? Is there a way to maybe retrieve the existing <code>JacksonJaxbJsonProvider</code> (that must have been registered implicitly somehow) or the existing <code>ObjectMapper</code> object and register the <code>JavaTimeModule</code> to it instead of creating a new Mapper object and a new Provider object?</p>