Im using Spring MVC 3.1 which by default uses Jackson for JSON-Java conversions.
I need to create custom conversions for the following:
- java.util.List (for all sorts of different types).
- Enums to strings
- java.util.Calendar
I have encountered a few different approaches for creating my custom converters:
As described here more or less, create a custom ObjectMapper:
public class CustomObjectMapper extends ObjectMapper...
Then adding MappingJacksonHttpMessageConverter to my servlet-context and register it with my custom ObjectMapper:
Then i guess I need to have some specific implementations in my custom objectMapper (how should it look like?).
- A second approach talks about extending FormattingConversionServiceFactoryBean and create my own converters (a class that implements org.springframework.core.convert.converter.Converter) as described here.
UPDATE 1: Another approach I have seen is to subcalss MappingJacksonHttpMessageConverter and override getJavaType method, as described here. To do this I will have to add my custom converter to the list of converters:
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<bean class="com.yl.app.CustomMappingJacksonHttpMessageConverter">
</list>
</property>
Which approach is the recommended one? Are these the only ones, is there a better way of doing it?
UPDATE 2:
I still don't get it. In practice, suppose I have class A and class B.
Class A holds a list of B:
public class A {
public List<B> list;
}
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)?