sappenin
sappenin

Reputation: 225

Force Spring 3.1 to use Jackson 2 when Jackson 1 JARs on the classpath

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?

Upvotes: 3

Views: 5611

Answers (2)

diffa
diffa

Reputation: 2996

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.

Upvotes: 1

sappenin
sappenin

Reputation: 225

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>

Upvotes: 4

Related Questions