title: Use two differently configured ObjectMappers in one Spring Boot application
body: <p>I am working on a middleware-app which deserializes values received via RestTemplate as json-String from a legacy-API (so, no influence on "their" data model and thus needing some custom config for my objectmapper consuming this api), and the app itself serves a restful API with (partially enriched and composited) data based on the legacydata as json, too.</p>
  
  <p>Now, my legacy-Mapping-Classes' Constructors are all sharing a common structure like this at the moment:</p>
  
  <pre><code>    ...
      private ObjectMapper mapper;
  
      public MyMapper() {
          this.mapper = new ObjectMapper();
          this.mapper.configure(DeserializationFeature.UNWRAP_ROOT_VALUE, true);
          this.mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
      }
      ...
  </code></pre>
  
  <p>because I use Jackson to de-serialize the json from the legacysystem. Basically I want to refactor this redundance using Springs DI Container. </p>
  
  <p>So I tried to create my own Objectmapper <code>@Component</code> which just extends <code>ObjectMapper</code> like it is stated in some answers in this thread:
  <a href="https://stackoverflow.com/questions/7854030/configuring-objectmapper-in-spring">Configuring ObjectMapper in Spring</a> - lets call it <code>FromLegacyObjectMapper</code> - instead of initializing my mapper in every class, so I created one and used</p>
  
  <pre><code>@Autowired
  private FromLegacyObjectMapper
  </code></pre>
  
  <p>(or the constructorinjection-equivalent, but for simplicitys sake..). 
  But this had some serious sideeffects. Actually, I wasn't able to deserialize clientjson to viewmodels in my controllers anymore because of the rootvalue-wrapping, because the autowiring overwrites the spring boot standard objectmapper which I actually need when deserializing viewModels from my frontend.</p>
  
  <p>I try to get it up and running like this:</p>
  
  <pre><code>frontend &lt;---&gt; My Api using Standard ObjectMapper &lt;--&gt; viewModel created by consuming legacy-Api-json using FromLegacyObjectMapper
  </code></pre>
  
  <p>So, what I surely could do is using a baseclass for my mappingclasses and just add the code above to the base constructor, and let every Mapperclass extend this base, but actually I hoped to find a way to use springs dependency injection container instead. I am out of ideas for now, so I hope anyone could help me out!</p>
  
  <p><em>edit</em>: To make it perhaps a bit clearer please see Moritz' answer below and our discussion in the comments. I am well aware I am able to use <code>@Qualifier</code> annotation, but this would just solve the problem if there is a way to add the @Qualifier to the standard objectmapper used in spring controllers. I'll do some research myself, but other answers are highly welcome.</p>