3

I have a Spring MVC 3.1 app built with Maven and I want to use Jackson 2 for JSON serialization/deserialization. I am including Jackson 2.x dependencies explicitly in my pom.xml file, although my app has a different dependency that uses Jackson 1.9.9 internally, and I can't easily remove that dependency.

Because of both Jackson libs being present on the classpath, it seems like Spring is defaulting to using Jackson version 1.9.9.

How do I force Spring MVC to use Jackson 2?

sappenin
  • 225
  • 3
  • 12

2 Answers2

4

To make this work, I had to adjust my mvc:annotation-driven xml configuration element to work as follows:

<mvc:annotation-driven>
  <mvc:message-converters register-defaults="false">
    <bean id="jacksonMessageConverter" 
      class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"/>
  </mvc:message-converters>
</mvc:annotation-driven>
sappenin
  • 225
  • 3
  • 12
1

Support for Jackson 2 was added in Spring 3.2 and back-ported to Spring 3.1.2. Once you have one of those versions you just need the jackson-databind library on the classpath as described here

Alternatively, Keith Donald included the source for MappingJackson2HttpMessageConverter in this gist as mentioned in this answer.

Community
  • 1
  • 1
diffa
  • 2,986
  • 1
  • 22
  • 35
  • I have both Jackson 1.9.9. and 2.1.2 libs on my classpath, and couldn't easily remove v1 of Jackson since it's used by a library that I need. Spring 3.1 seems to preference Jackson 1 over 2 when both are present, so I had to explicitly tell Spring to use v2 of Jackson as per my answer. – sappenin Jan 16 '13 at 05:07