用Jackson和Spring序列化Joda DateTime [英] Serializing Joda DateTime with Jackson and Spring
问题描述
我遇到问题,一直使用Spring Boot和Jackson-databind 2.5.2将Joda DateTime从java序列化和反序列化为json,然后再将其反序列化。我的pom.xml看起来像这样。
I am having problems consistently serializing and deserializing a Joda DateTime from java to json and back again using Spring Boot and Jackson-databind 2.5.2. My pom.xml looks like this.
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.5.2</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-joda</artifactId>
<version>2.5.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>1.2.1.RELEASE</version>
</dependency>
当我序列化DateTime对象时,我得到一个表示DateTime的整数。不是我的预期,但很好。但是当我去保存我的对象时,我得到以下错误...
When I serialize the DateTime object I get an integer representing the DateTime. Not what I expected actually, but fine. BUT when I go to save my object back I get the following error...
Failed to convert property value of type 'java.lang.String' to required type 'org.joda.time.DateTime' for property 'endTime';
nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type java.lang.String to type org.joda.time.DateTime for value '1428600998511'
由于某种原因,它将序列化为一个整数,然后将其反序列化,就像它是一个字符串一样,但它不是。我还尝试在调用其余服务之前设置endTime = new Date(intValue),并且尝试将字符串转换为'Tue Apr 28 2015 00:00:00 GMT-0700(PDT)'到DateTime也失败了。
For some reason it is serializing it to an integer but then deserializing it as if it's a string, which it is not. I also tried setting the endTime = new Date(intValue) before calling the rest service and that also failed trying to convert a string like 'Tue Apr 28 2015 00:00:00 GMT-0700 (PDT)' to a DateTime.
我做错了什么?
更新:
以下是我尝试立即POST的和的JSON。
Here is the JSON that I GET and that I try to immediately POST right back.
{
id: 4,
username: "",
name: "eau",
email: "aoue",
verbatimLocation: null,
latitude: null,
longitude: null,
startTime:null,
endTime: 1429034332312,
description: "ueoa",
media: [ ],
timeSubmitted: 1428600998000,
status: null,
submissionid: null
}
推荐答案
对于更可重用的机制,您可以创建一个 JsonSerializer
:
For a more re-usable mechanism, you can create a JsonSerializer
:
/**
* When passing JSON around, it's good to use a standard text representation of
* the date, rather than the full details of a Joda DateTime object. Therefore,
* this will serialize the value to the ISO-8601 standard:
* <pre>yyyy-MM-dd'T'HH:mm:ss.SSSZ</pre>
* This can then be parsed by a JavaScript library such as moment.js.
*/
public class JsonJodaDateTimeSerializer extends JsonSerializer<DateTime> {
private static DateTimeFormatter formatter = ISODateTimeFormat.dateTime();
@Override
public void serialize(DateTime value, JsonGenerator gen, SerializerProvider arg2)
throws IOException, JsonProcessingException {
gen.writeString(formatter.print(value));
}
}
然后你可以注释你的 get
方法:
Then you can annotate your get
methods with:
@JsonSerialize(using = JsonJodaDateTimeSerializer.class)
这使您可以在整个应用程序中保持一致的格式,而无需在任何地方重复文本模式。这也是时区意识。
This gives you consistent formatting throughout your application, without repeating text patterns everywhere. It's also timezone aware.
这篇关于用Jackson和Spring序列化Joda DateTime的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!