title: Spring 3.1 custom JSON converters
body: <p>Im using Spring MVC 3.1 which by default uses Jackson for JSON-Java conversions.</p>
  
  <p>I need to create custom conversions for the following:</p>
  
  <ol>
  <li>java.util.List (for all sorts of different types).</li>
  <li>Enums to strings</li>
  <li>java.util.Calendar</li>
  </ol>
  
  <p>I have encountered a few different approaches for creating my custom converters:</p>
  
  <ol>
  <li><p>As described <a href="http://magicmonster.com/kb/prg/java/spring/webmvc/jackson_custom.html" rel="nofollow noreferrer">here</a> more or less, create a custom ObjectMapper:</p>
  
  <p>public class CustomObjectMapper extends ObjectMapper...</p></li>
  </ol>
  
  <p>Then adding MappingJacksonHttpMessageConverter to my servlet-context and register it with my custom ObjectMapper:</p>
  
  <p>
        
            
                
            
        
    </p>
  
  <p>Then i guess I need to have some specific implementations in my custom objectMapper (how should it look like?).</p>
  
  <ol start="2">
  <li>A second approach talks about extending FormattingConversionServiceFactoryBean and create my own converters (a class that implements org.springframework.core.convert.converter.Converter) as described <a href="http://jpgmr.wordpress.com/tag/conversion-service/" rel="nofollow noreferrer">here</a>.</li>
  </ol>
  
  <p>UPDATE 1:
  Another approach I have seen is to subcalss MappingJacksonHttpMessageConverter and override getJavaType method, as described <a href="https://stackoverflow.com/a/6173511/887343">here</a>. To do this I will have to add my custom converter to the list of converters:</p>
  
  <pre><code>&lt;bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"&gt;
  &lt;property name="messageConverters"&gt;
      &lt;list&gt;
          &lt;bean class="com.yl.app.CustomMappingJacksonHttpMessageConverter"&gt;
      &lt;/list&gt;
  &lt;/property&gt;
  </code></pre>
  
  <p></p>
  
  <p>Which approach is the recommended one? Are these the only ones, is there a better way of doing it?</p>
  
  <p><strong>UPDATE 2:</strong></p>
  
  <p>I still don't get it. In practice, suppose I have class A and class B.</p>
  
  <p>Class A holds a list of B:</p>
  
  <pre><code>public class A {
  
     public List&lt;B&gt; list;
  
  }
  </code></pre>
  
  <p>I would like to return an object of type A to my client. What should I do in my server besides adding the @ResponseBody annotation (which is not enough in this case as far as I get it)?</p>