title: Spring MVC + Jackson: Dynamic Json serialization
body: <p>I'm using Spring MVC 3.0.5 with Jackson 1.7.2.</p>
  
  <p>I wish to implement a Dynamic Bean serializer assignment mechanism, for example, let's say that my MVC Controller returns (<code>@ResponseBody</code>) an Object of type MyObject. By default, Jackson's SerializerFactory will look for the most appropriate Serializer, including my custom Serializers (like for example <code>CustomSerializer extends JsonSerializer&lt;MyObject&gt;</code>).</p>
  
  <p>However, I want my custom Serializers to be triggered just in case some flag is active (let's say, a boolean variable attached to ThreadLocal). Otherwise, I want to use Jackson provided Serializers, keeping <code>MappingJacksonHttpMessageConverter</code>'s default behaviour intact.</p>
  
  <p>Is there any way to implement an approach to that?</p>
  
  <p>I've already registered my own ObjectMapper, SerializerFactory and CustomSerializers into Spring's <code>&lt;mvc:annotaion-driven /&gt;</code> default <code>MappingJacksonHttpMessageConverter</code>.</p>
  
  <pre><code>public class ConvertingPostProcessor implements BeanPostProcessor {
  
      private ObjectMapper jacksonJsonObjectMapper;
  
      public Object postProcessBeforeInitialization(Object bean, String name)
              throws BeansException {
          if (bean instanceof AnnotationMethodHandlerAdapter) {
              HttpMessageConverter&lt;?&gt;[] convs = ((AnnotationMethodHandlerAdapter) bean).getMessageConverters();
              for (HttpMessageConverter&lt;?&gt; conv: convs) {
                  if (conv instanceof MappingJacksonHttpMessageConverter) {
                      ((MappingJacksonHttpMessageConverter) conv).setObjectMapper(jacksonJsonObjectMapper);
                  }
              }
          }
          return bean;
      }
  
      public Object postProcessAfterInitialization(Object bean, String name)
              throws BeansException {
          return bean;
      }
  
      public void setJacksonJsonObjectMapper(ObjectMapper jacksonJsonObjectMapper) {
          this.jacksonJsonObjectMapper = jacksonJsonObjectMapper;
      }
  }
  </code></pre>
  
  <p>And spring-mvc.xml would be:</p>
  
  <pre><code>&lt;mvc:annotation-driven /&gt;
  ...
      &lt;bean id="jacksonJsonObjectMapper" class="org.mycode.serialize.CustomObjectMapper"&gt;
          &lt;property name="customSerializerFactory" ref="jacksonJsonCustomSerializerFactory" /&gt;
      &lt;/bean&gt;
  
      &lt;bean id="jacksonJsonCustomSerializerFactory" class="org.mycode.serialize.CustomSerializerFactoryRegistry"&gt;
          &lt;property name="serializers"&gt;
              &lt;map&gt;
                  &lt;entry key="org.mycode.domain.MyObject" value-ref="customSerializer" /&gt;         
              &lt;/map&gt;
          &lt;/property&gt;
      &lt;/bean&gt;
  
      &lt;bean id="customSerializer" class="org.mycode.serialize.CustomSerializer"&gt;
          &lt;property name="jacksonJsonCustomSerializerFactory" ref="jacksonJsonCustomSerializerFactory" /&gt;
      &lt;/bean&gt;
  
      &lt;bean id="convertingPostProcessor" class="org.mycode.serialize.ConvertingPostProcessor"&gt;
          &lt;property name="jacksonJsonObjectMapper" ref="jacksonJsonObjectMapper" /&gt;
      &lt;/bean&gt; 
  </code></pre>
  
  <p>Thanks in advance!!</p>