Deserialization Issues with Joda Time / Jackson 2 / Spring - Unable to create value of type [simple type, class org.joda.time.DateTime]

I get an exception when trying to deserialize a JSON string containing date strings in POJO using Joda.

I am using Jackson2 with Spring and Robospice.

I get the following exception:

Failed to read JSON: cannot create value of type [simple type, class org.joda.time.DateTime] from String value ('2014-07-25T00: 00: 00'); no constructor String / factory method

Here is the code I currently have:

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); 

POJO fields are defined as follows:

 private DateTime departureDate; 

This worked for me in Jackson1 ... but it doesn't seem to work in Jackson2.

+7
jackson spring-android jodatime
source share
2 answers

I believe that my problem was that I used different versions of the Jackson Joda component for everything else.

I ended up doing this in the 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 
+5
source share

For Maven user: this problem occurs when you use jackson and joda, but forget to include jackson-datatype-joda. For the latest jackson version with this answer (2.6.3), there are dependencies that you should 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> 
+9
source share

All Articles