17
votes

v2.1.1, joda module.

I can convert a json file to a pojo in a unit test using objectMapper.readValue(file, pojo .class);

However, when a Spring RESTTemplate client invokes the default json converter to convert an inputStream containing the domain object with Joda types (DateTime or LocalDate), it generates an error: objectMapper.readValue(httpInputMessage.getBody(), javaType)

   
com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of org.joda.time.DateTime out of START_OBJECT token
at Source: org.mortbay.jetty.HttpParser$Input@46a09b; line: 1, column: 752
at com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:164)
at com.fasterxml.jackson.databind.DeserializationContext.mappingException(DeserializationContext.java:599)
at com.fasterxml.jackson.databind.DeserializationContext.mappingException(DeserializationContext.java:593)
at com.fasterxml.jackson.datatype.joda.deser.DateTimeDeserializer.deserialize(DateTimeDeserializer.java:51)
at com.fasterxml.jackson.datatype.joda.deser.DateTimeDeserializer.deserialize(DateTimeDeserializer.java:21)
at com.fasterxml.jackson.databind.deser.SettableBeanProperty.deserialize(SettableBeanProperty.java:375)
at com.fasterxml.jackson.databind.deser.BeanDeserializer._deserializeUsingPropertyBased(BeanDeserializer.java:559)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObjectUsingNonDefault(BeanDeserializer.java:393)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:289)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:121)
at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:226)
at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:203)
at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:23)
at com.fasterxml.jackson.databind.deser.SettableBeanProperty.deserialize(SettableBeanProperty.java:375)
at com.fasterxml.jackson.databind.deser.impl.MethodProperty.deserializeSetAndReturn(MethodProperty.java:106)
at com.fasterxml.jackson.databind.deser.BuilderBasedDeserializer.deserializeFromObject(BuilderBasedDeserializer.java:326)
at com.fasterxml.jackson.databind.deser.BuilderBasedDeserializer.deserialize(BuilderBasedDeserializer.java:143)
at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:226)
at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:203)
at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:23)
at com.fasterxml.jackson.databind.deser.SettableBeanProperty.deserialize(SettableBeanProperty.java:375)
at com.fasterxml.jackson.databind.deser.impl.MethodProperty.deserializeSetAndReturn(MethodProperty.java:106)
at com.fasterxml.jackson.databind.deser.BuilderBasedDeserializer.deserializeFromObject(BuilderBasedDeserializer.java:

Same issue occurs with LocalDate

com.fasterxml.jackson.databind.JsonMappingException: Unexpected token (START_OBJECT), expected START_ARRAY: expected JSON Array, String or Number
at Source: org.mortbay.jetty.HttpParser$Input@d297c0; line: 1, column: 51
at com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:164)
at com.fasterxml.jackson.databind.DeserializationContext.wrongTokenException(DeserializationContext.java:692)
at com.fasterxml.jackson.datatype.joda.deser.LocalDateDeserializer.deserialize(LocalDateDeserializer.java:50)
....

Why is the call chain passing a START_OBJECT in one case and START_ARRAY in the other?

4
did you manage to get this fixed? I'm having the same problem with deserialization in LiftAdrian

4 Answers

20
votes

To solve a similar problem I did the following,

I downloaded jackson-datatype-joda-2.2.1.jar from http://mvnrepository.com/artifact/com.fasterxml.jackson.datatype/jackson-datatype-joda/2.2.1 if you are using maven the dependency definition is there too.

then I added an annotation for every LocalDate field in my app as follows:

@JsonDeserialize(using=LocalDateDeserializer.class)
private LocalDate releasedDate;

the imports look like this:

import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.datatype.joda.deser.LocalDateDeserializer;

And the problem vanished.

Hope this helps!

6
votes

I think this has to do with some discrepancy between serializer and deserializer; so that one produces the other (I think Joda module actually writes an array of ints, when registered). This would most likely be because either deserializer or serializer registration is missing.

By default, without any extra handling, Jackson would consider Joda types just POJOs, and use getters/setters. But Joda module uses more compact representations (Strings, arrays). So what may be happening is that serialization side is not using Joda module; and deserialization is.

3
votes

you should be using both serialization and de-serialization for joda-time; against those fields which you are trying to save and fetch.
By doing so, we will give both side responsibilities to jackson (java to mongo/json and mongo/json to java conversions).

Sample code:

@JsonDeserialize(using= LocalDateDeserializer.class)
@JsonSerialize(using = LocalDateSerializer.class)
LocalDate dateFieldToBeConverted

and these are the imports:

import com.fasterxml.jackson.databind.annotation.JsonDeserialize

import com.fasterxml.jackson.databind.annotation.JsonSerialize

import com.fasterxml.jackson.datatype.joda.deser.LocalDateDeserializer

import com.fasterxml.jackson.datatype.joda.ser.LocalDateSerializer

import org.joda.time.LocalDate

After doing this, simple save and findOne queries on mongo (through java repo classes) will work without any issues. Hope this helps.

0
votes

The key issue here is that Spring is converting JodaTime in a different way then for example Jersey.

This is what i had to do in my app-servlet.xml:

<mvc:annotation-driven>
    <mvc:message-converters>
        <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
            <property name="objectMapper">
                <bean class="no.bouvet.jsonclient.spring.JsonClientJackson2ObjectMapperFactoryBean"/>
            </property>
        </bean>
    </mvc:message-converters>
</mvc:annotation-driven>

Here you can find the no.bouvet.jsonclient.spring.JsonClientJackson2ObjectMapperFactoryBean from the java-json-client library