Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 746
Chris Nevill Avatar asked Aug 10 '14 19:08

Chris Nevill


2 Answers

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>
like image 168
Polorumpus Avatar answered Nov 18 '22 12:11

Polorumpus


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
like image 8
Chris Nevill Avatar answered Nov 18 '22 13:11

Chris Nevill