title: Registrer MappingJackson2HttpMessageConverter in Spring 3.1.2 with JAXB annotations
body: <p>I have a number of entities with JAXB annotations that I would like to convert to JSON using a message-converter.</p>
  
  <p>I know that my ObjectMapper that reads the JAXB annotations works:</p>
  
  <pre><code>String correctJsonText = jacksonObjectMapper.writeValueAsString(entityWithJAXB); 
  </code></pre>
  
  <p>But when i call my rest service the default registered MappingJacksonHttpMessageConverter   (which is not configured for reading JAXB) seems to take over - resulting in a stackoverflow due to cyclic references when @XmlTransient is ignored...</p>
  
  <p>How do i configure Spring to use MappingJackson2HttpMessageConverter ?</p>
  
  <p><strong>Current Configuration</strong></p>
  
  <pre><code>&lt;mvc:annotation-driven&gt;
      &lt;mvc:message-converters register-defaults="false"&gt;
          &lt;bean id="jacksonMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"&gt;
              &lt;property name="objectMapper" ref="jacksonObjectMapper" /&gt;
              &lt;property name="supportedMediaTypes"&gt;
                  &lt;list&gt;
                      &lt;bean class="org.springframework.http.MediaType"&gt;
                          &lt;constructor-arg index="0" value="application" /&gt;
                          &lt;constructor-arg index="1" value="json" /&gt;
                          &lt;constructor-arg index="2" value="UTF-8" /&gt;
                      &lt;/bean&gt;
                  &lt;/list&gt;
              &lt;/property&gt;
  
          &lt;/bean&gt;
      &lt;/mvc:message-converters&gt;
  &lt;/mvc:annotation-driven&gt;
  
  &lt;bean id="jaxbAnnotationInspector" class="com.fasterxml.jackson.module.jaxb.JaxbAnnotationIntrospector" /&gt;
  
  &lt;bean id="jacksonObjectMapper" class="com.fasterxml.jackson.databind.ObjectMapper"&gt;
      &lt;property name="annotationIntrospector" ref="jaxbAnnotationInspector" /&gt;
  &lt;/bean&gt;
  </code></pre>
  
  <p><strong>REST service</strong></p>
  
  <pre><code>@RequestMapping(value="/{id}", method=RequestMethod.GET, produces = "application/json;charset=UTF-8")
  public @ResponseBody EntityWithJAXB readEntityWithJAXB(@PathVariable int id, Model model) {
      return entityService.getById(id);
  }
  </code></pre>
  
  <p><strong>Dependencies</strong></p>
  
  <pre><code>&lt;dependency&gt;
      &lt;groupId&gt;com.fasterxml.jackson.core&lt;/groupId&gt;
      &lt;artifactId&gt;jackson-core&lt;/artifactId&gt;
      &lt;version&gt;2.0.5&lt;/version&gt;
  &lt;/dependency&gt;
  &lt;dependency&gt;
      &lt;groupId&gt;com.fasterxml.jackson.core&lt;/groupId&gt;
      &lt;artifactId&gt;jackson-databind&lt;/artifactId&gt;
      &lt;version&gt;2.0.5&lt;/version&gt;
  &lt;/dependency&gt;
  &lt;dependency&gt;
      &lt;groupId&gt;com.fasterxml.jackson.module&lt;/groupId&gt;
      &lt;artifactId&gt;jackson-module-jaxb-annotations&lt;/artifactId&gt;
      &lt;version&gt;2.0.5&lt;/version&gt;
  &lt;/dependency&gt;
  </code></pre>
  
  <p><strong>UPDATE / Solution</strong></p>
  
  <p>By debugging my context I could see that the configuration in <code>&lt;mvc:annotation-driven&gt;</code> had no effect. </p>
  
  <p>It turned out that my unit test with MockMcv always loaded the default handleradapters, thus ignoring my custom objectmapper. Due to convenience I only tested the controller using junit test, since it hit the controller just fine I did not think of this as a probable error cause...</p>
  
  <p>I did not find a fix for my test yet, but when I call the service using curl everything works!</p>
  
  <p><strong>UPDATE / Final solution</strong></p>
  
  <p>Just found a solution for my test setup; when using MockMvc (spring-test-mvc) you must specify custom message-converters explicit:</p>
  
  <pre><code>private MockMvc mockMvc;
  
  @Autowired
  private MappingJackson2HttpMessageConverter jacksonMessageConverter;
  
  @Autowired
  private RestController restController;
  
  @Before
  public void initMockMvc(){
      mockMvc = MockMvcBuilders.standaloneSetup(restController)
              .setMessageConverters(jacksonMessageConverter).build();
  } 
  
  @Test
  public void testRestController() throws Exception{
      DefaultRequestBuilder requestBuilder = MockMvcRequestBuilders.get("/json/42");
      this.mockMvc.perform(requestBuilder).andDo(print()).andExpect(status().isOk());
  }
  </code></pre>
  
  <p>The only remaining issue is that the <code>jacksonMessageConverter</code> can't be autowired directly until the following JIRA is resolved: <a href="https://jira.springsource.org/browse/SPR-9469" rel="nofollow">https://jira.springsource.org/browse/SPR-9469</a>. Until then, I have just created a copy of the jacksonMessageConverter in my test context.</p>