title: Spring 3.2 and Jackson 2: add custom object mapper
body: <p>I'm developing a REST webservice in spring MVC. I need to change how jackson 2 serialize mongodb objectids. I'm not sure of what to do because I found partial documentation for jackson 2, what I did is to create a custom serializer:</p>
  
  <pre><code>public class ObjectIdSerializer extends JsonSerializer&lt;ObjectId&gt; {
  
  
      @Override
      public void serialize(ObjectId value, JsonGenerator jsonGen,
              SerializerProvider provider) throws IOException,
              JsonProcessingException {
          jsonGen.writeString(value.toString());
      }
  }
  </code></pre>
  
  <p>Create a ObjectMapper</p>
  
  <pre><code>public class CustomObjectMapper extends ObjectMapper {
  
      public CustomObjectMapper() {
          SimpleModule module = new SimpleModule("ObjectIdmodule");
          module.addSerializer(ObjectId.class, new ObjectIdSerializer());
          this.registerModule(module);
      }
  
  }
  </code></pre>
  
  <p>and then register the mapper</p>
  
  <pre><code>&lt;mvc:annotation-driven&gt;
      &lt;mvc:message-converters register-defaults="true"&gt;
          &lt;bean
              class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"&gt;
              &lt;property name="objectMapper"&gt;
                  &lt;bean class="my.package.CustomObjectMapper"&gt;&lt;/bean&gt;
              &lt;/property&gt;
          &lt;/bean&gt;
      &lt;/mvc:message-converters&gt;
  &lt;/mvc:annotation-driven&gt;
  </code></pre>
  
  <p>My CustomConverter is never called. I think the CustomObjectMapper definition is wrong,I adapted it from some code for jackson 1.x</p>
  
  <p>In my controllers I'm using @ResponseBody.
  Where am I doing wrong? Thanks</p>