Search code examples
jacksonjodatimespring-android

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


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.


Solution

  • I believe my issue was simply that I was running different versions of the jackson joda component to everything else.

    In the end I did this in my Gradle file

    String jacksonCore = 'com.fasterxml.jackson.core:jackson-core:'
    String jacksonAnnotations = 'com.fasterxml.jackson.core:jackson-annotations:'
    String jacksonDatabind = 'com.fasterxml.jackson.core:jackson-databind:'
    String jacksonJoda='com.fasterxml.jackson.datatype:jackson-datatype-joda:'
    String jacksonVersion = '2.4.1'
    
    
    dependencies {
    
        compile jacksonCore + jacksonVersion
        compile jacksonAnnotations + jacksonVersion
        compile jacksonDatabind + jacksonVersion
        compile jacksonJoda + jacksonVersion