30

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-type still work?

DeejUK
  • 12,891
  • 19
  • 89
  • 169

6 Answers6

24

Keith Donald (of spring source) tweeted the following a while back.

Spring MVC support for Jackson 2; also works with Jackson's native "pretty print" feature https://gist.github.com/2423129

I haven't tried the MappingJackson2HttpMessageConverter found in the gist above but it would surprise me if it did not work.

Fredrik
  • 416
  • 3
  • 6
  • 5
    Yes it works, they added the MappingJackson2HttpMessageConverter class that is meant to be used instead of the "old" MappingJacksonHttpMessageConverter – emas Nov 08 '12 at 14:04
22

Support for Jackson 2 has been added in Spring 3.2, and has also backported to Spring 3.1.2 (SPR-9507)

Aleksander Blomskøld
  • 18,374
  • 9
  • 76
  • 82
  • 2
    You'll need to change your pom.xml if you're migrating from 1.x (`Step 1: Update Maven / JAR dependencies` from http://www.cowtowncoder.com/blog/archives/2012/04/entry_469.html is all you need for the builtin support to latch on) – Manav Jul 20 '12 at 09:28
17

Since Spring 3.1.2 you simply have to add jackson-databind jar to your classpath.

In Maven you can configure it like this:

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>${jackson.version}</version>
</dependency>
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-core</artifactId>
    <version>${jackson.version}</version>
</dependency>

The rest of the magic will be done by spring automatically.

See: http://norrisshelton.wordpress.com/2012/07/15/spring-3-mvc-with-json-via-jackson-2-0/

Michael K.
  • 497
  • 5
  • 12
9

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;
    }
}
Manav
  • 10,094
  • 6
  • 44
  • 51
  • I'm sure that this is not the official way but it's very clever! – Roy Truelove Oct 03 '12 at 18:26
  • 3
    The "official" method of configuring this is described here: http://wallsofchange.wordpress.com/2013/02/02/spring-mvc-rest-services-force-jackson-to-serialize-dates-as-iso-8601-dates/. The only difference for Jackson 2 is that you have to use `MappingJackson2HttpMessageConverter` instead of `MappingJacksonHttpMessageConverter` and `SerializationFeature.WRITE_DATES_AS_TIMESTAMPS` instead of `SerializationConfig.Feature.WRITE_DATES_AS_TIMESTAMPS`. – Ryan Walls Feb 19 '13 at 21:21
2

To be clear, Spring 3.1 doesn't have native support for Jackson 2. It's in Spring 3.2

Marc
  • 6,773
  • 10
  • 44
  • 68
0

I didn't test Spring MVC 3.1 compatible with Jackson 2.0. But I encounter 3.0.5 compatible issue. Finally I find Spring MVC 3.0.5 is only compatible with Jackson 1.0.0, definitely Jackson 1.5.3 isn't compatible!!!

malajisi
  • 2,165
  • 1
  • 22
  • 18