Is Spring MVC 3.1 compatible with Jackson 2.0? Will Spring MVC\'s automatic detection of Jackson on the classpath, and delegation to Jackson for requests with a JSON content
For Spring 3.1.2 and Jackson 2 -
As outlined above, the automatic support JustWorks™
but configuration doesn't, as most of the web is littered with pre Spring3/Jackson2 configuration mechanisms
So for posterity, I'll list out a hack(? or is this the official way) to configure the Jackson converter. In this particular case, I am configuring the converter to return dates in the ISO-8601 format:
package foo.bar.JacksonConfig;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.stereotype.Component;
@Component
public class JacksonConfig implements BeanPostProcessor {
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
return bean;
}
public Object postProcessAfterInitialization(Object bean, String beanName)
throws BeansException {
if (bean instanceof MappingJackson2HttpMessageConverter) {
MappingJackson2HttpMessageConverter jsonConverter =
(MappingJackson2HttpMessageConverter) bean;
ObjectMapper objectMapper = jsonConverter.getObjectMapper();
objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
jsonConverter.setObjectMapper(objectMapper);
}
return bean;
}
}