java – joda. time. Datetime deserialization error

I tried to deserialize a class with datetime as attibute:

import org.joda.time.DateTime;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.datatype.joda.deser.DateTimeDeserializer;
import com.fasterxml.jackson.datatype.joda.ser.DateTimeSerializer;

class MyClass {

    private DateTime alertTimestamp;

    private String name;

    @JsonSerialize(using = DateTimeSerializer.class)
    public DateTime getAlertTimestamp() {
        return alertTimestamp;
    }

    @JsonDeserialize(using = DateTimeDeserializer.class)
    public void setAlertTimestamp(DateTime Now) {
        this.alertTimestamp = Now;
    }

    //...
}

But when I try to deserialize, I have this exception:

com.fasterxml.jackson.datatype.joda.deser.DateTimeDeserializer has no default (no arg) constructor

I use it to deserialize:

ObjectMapper mapper = new ObjectMapper();
mapper.readValue(jsonData,MyClass.class);

And an example of my jsondata:

{    
    "name":"test","alertTimestamp":       {"year":2014,"era":1,"dayOfMonth":24,"dayOfWeek":1,"dayOfYear":83,"weekOfWeekyear":13,"weekyear":2014,"monthOfYear":3,"yearOfEra":2014,"yearOfCentury":14,"centuryOfEra":20,"millisOfSecond":232,"millisOfDay":45143232,"secondOfMinute":23,"secondOfDay":45143,"minuteOfHour":32,"minuteOfDay":752,"hourOfDay":12,"zone":{"uncachedZone":{"cachable":true,"fixed":false,"id":"America/Los_Angeles"},"millis":1395689543232,"chronology":{"zone":{"uncachedZone":{"cachable":true,"id":"America/Los_Angeles"}},"afterNow":false,"beforeNow":false,"equalNow":true}
}

Solution

@Jsondeserialize requires a jsondeserializer with a parameterless constructor The latest version of datetimedeserializer does not have such a constructor

If you have modified the format, that is Alerttimestamp should be just a timestamp, and then you can simply register jodamodule with objectmapper It will use datetimedeserializer. Inside the datetime field You can delete the @ jsondeserialize comment

mapper.registerModule(new JodaModule());

You need to add the Jackson - datatype - joda library

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>