joda.time.DateTime反序列化错误
我试图反序列化以DateTime作为修饰符的类:
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;
}
//...
}
但是,当我尝试tro反序列化时,却遇到以下异常:
com.fasterxml.jackson.datatype.joda.deser.DateTimeDeserializer has no default (no arg) constructor
我用它来反序列化:
ObjectMapper mapper = new ObjectMapper();mapper.readValue(jsonData, MyClass.class);
还有我的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"},"fixed":false,"id":"America/Los_Angeles"},"millis":1395689543232,"chronology":{"zone":{"uncachedZone":{"cachable":true,"fixed":false,"id":"America/Los_Angeles"},"fixed":false,"id":"America/Los_Angeles"}},"afterNow":false,"beforeNow":false,"equalNow":true}
}
回答:
@JsonDeserialize
期望JsonDeserializer
使用无参数构造函数。的最新版本DateTimeDeserializer
没有这样的构造函数。
如果您已固定格式,即。alertTimestamp
应该只是一个时间戳,那么你可以简单地注册JodaModule
与ObjectMapper
。它将在DateTimeDeserializer
内部用于DateTime
字段。您可以摆脱@JsonDeserialize
注释。
mapper.registerModule(new JodaModule());
您需要添加jackson-datatype-joda
库。
以上是 joda.time.DateTime反序列化错误 的全部内容, 来源链接: utcz.com/qa/400478.html