Deserialization issues with Joda Time / Jackson 2 / Spring - Can not instantiate value of type [simple type, class org.joda.time.DateTime]

Chris Nevill picture Chris Nevill · Aug 10, 2014 · Viewed 17k times · Source

I'm getting an exception when attempting to deserialize an JSON string which contains date strings to a POJO using Joda.

I'm using Jackson2 with Spring and Robospice.

I'm getting the following exception:

Could not read JSON: Can not instantiate value of type [simple type, class org.joda.time.DateTime] from String value ('2014-07-25T00:00:00'); no single-String constructor/factory method

Here's the code I have at present:

MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter 
                            = new MappingJackson2HttpMessageConverter();

mappingJackson2HttpMessageConverter.getObjectMapper().registerModule(new JodaModule());
msgConverters.add(mappingJackson2HttpMessageConverter);

restTemplate.setMessageConverters(msgConverters);
HttpEntity<?> httpEntity = new HttpEntity<Object>(headers);

final ResponseEntity<HolidayList> responseEntity 
            = restTemplate.exchange(url, HttpMethod.GET, httpEntity,HolidayList.class);

The POJO fields are defined like so:

private DateTime departureDate;

I had this working in Jackson1... but can't seem to get it working in Jackson2.

Answer

Polorumpus picture Polorumpus · Nov 21, 2015

For Maven user: This problem occurs when you are using jackson and joda but forget to include jackson-datatype-joda. For latest jackson version as of this answer (2.6.3), following is the dependencies that you have to include in your pom file.

    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-core</artifactId>
        <version>2.6.3</version>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.6.3</version>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.datatype</groupId>
        <artifactId>jackson-datatype-joda</artifactId>
        <version>2.6.3</version>
    </dependency>