title: How to autowire default XmlMapper in Spring Boot application
body: <p>I'm having some issues autowiring the default Jackson <code>XmlMapper</code> in one of my Spring Boot projects. I've created a simple example project that illustrates this.</p>
  
  <p>What I'm doing here is roughly based on this:</p>
  
  <p><a href="http://docs.spring.io/spring-boot/docs/1.2.2.RELEASE/reference/htmlsingle/#howto-write-an-xml-rest-service" rel="noreferrer">http://docs.spring.io/spring-boot/docs/1.2.2.RELEASE/reference/htmlsingle/#howto-write-an-xml-rest-service</a></p>
  
  <p><a href="http://docs.spring.io/spring-boot/docs/1.2.2.RELEASE/reference/htmlsingle/#howto-customize-the-jackson-objectmapper" rel="noreferrer">http://docs.spring.io/spring-boot/docs/1.2.2.RELEASE/reference/htmlsingle/#howto-customize-the-jackson-objectmapper</a></p>
  
  <p>From pom.xml</p>
  
  <pre><code>&lt;!-- ... --&gt;
  
  &lt;parent&gt;
      &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
      &lt;artifactId&gt;spring-boot-starter-parent&lt;/artifactId&gt;
      &lt;version&gt;1.2.2.RELEASE&lt;/version&gt;
  &lt;/parent&gt;
  
  &lt;!-- ... --&gt;
  
  &lt;dependencies&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
          &lt;artifactId&gt;spring-boot-starter&lt;/artifactId&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
          &lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;com.fasterxml.jackson.dataformat&lt;/groupId&gt;
          &lt;artifactId&gt;jackson-dataformat-xml&lt;/artifactId&gt;
      &lt;/dependency&gt;
  &lt;/dependencies&gt;
  
  &lt;!-- ... --&gt;
  </code></pre>
  
  <p>The main class:</p>
  
  <pre><code>@SpringBootApplication
  public class DemoApplication {
  
      public static void main(String[] args) {
          SpringApplication.run(DemoApplication.class, args);
      }
  }
  </code></pre>
  
  <p>The Demo POJO, not specifying <code>@XmlRootElement</code>, so it won't use JAXB:</p>
  
  <pre><code>@JsonInclude(Include.NON_NULL)
  public class Demo {
      private String stringProperty;
      private int intProperty;
      public String getStringProperty() {
          return stringProperty;
      }
      public void setStringProperty(String stringProperty) {
          this.stringProperty = stringProperty;
      }
      public int getIntProperty() {
          return intProperty;
      }
      public void setIntProperty(int intProperty) {
          this.intProperty = intProperty;
      }
  }
  </code></pre>
  
  <p>The Demo Controller:</p>
  
  <pre><code>@RestController
  public class DemoController {
      @Autowired 
      private ObjectMapper objectMapper;
      // @Autowired 
      // private XmlMapper xmlMapper;
  
      @RequestMapping(value = "/demo", method = RequestMethod.GET)
      public Demo getDemo() {
          Demo demo = new Demo();
          demo.setStringProperty("Hello world!");
          demo.setIntProperty(42);
          return demo;
      }
  }
  </code></pre>
  
  <p>Everything works fine the way it is, depending on <code>Accept</code> headers, either JSON or XML will be returned.</p>
  
  <p>I can easily autowire the default <code>ObjectMapper</code> configured by Spring Boot. So far so good.</p>
  
  <p>If I comment in the autowiring of the <code>XmlMapper</code> I get:</p>
  
  <pre><code>Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.fasterxml.jackson.dataformat.xml.XmlMapper] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
  at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1301)
  at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1047)
  at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:942)
  at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:533)
  ... 18 more
  </code></pre>
  
  <p>Any idea why this is? I would have assumed it to work the same way as the <code>ObjectMapper</code>. Just to clarify, I <em>don't</em> want to customise the mappers, I simply want a reference to the default ones created by Spring Boot.</p>