According to http://wiki.fasterxml.com/JacksonFAQDateHandling, “DateTime can be automatically serialized/deserialized similar to how java.util.Date is handled.” However, I am not able to accomplish this automatic functionality. There are StackOverflow discussions related to this topic yet most involve a code-based solution, but based upon the quote above I should be able to accomplish this via simple configuration.
每http://wiki.fasterxml.com/JacksonFAQDateHandling次,我都会设置配置,以便将日期写入时间戳是错误的.结果就是java.util.日期类型序列化为ISO 8601格式,但不包括组织.乔达.时间DateTime类型被序列化为长对象表示形式.
My environment is this:
杰克逊2.1
我的jsonMapper bean的Spring配置是
@Bean
public ObjectMapper jsonMapper() {
ObjectMapper objectMapper = new ObjectMapper();
//Fully qualified path shows I am using latest enum
ObjectMapper.configure(com.fasterxml.jackson.databind.SerializationFeature.
WRITE_DATES_AS_TIMESTAMPS , false);
return objectMapper;
}
我的测试代码片段如下
Date d = new Date();
DateTime dt = new DateTime(d); //Joda time
Map<String, Object> link = new LinkedHashMap<String, Object>();
link.put("date", d);
link.put("createdDateTime", dt);
The resulting snippet of JSON output is this:
{"date":"2012-12-24T21:20:47.668+0000"}
{"createdDateTime": {"year":2012,"dayOfMonth":24,"dayOfWeek":1,"era":1,"dayOfYear":359,"centuryOfEra":20,"yearOfEra":2012,"yearOfCentury":12,"weekyear":2012,"monthOfYear":12 *... remainder snipped for brevity*}}
我的期望是DateTime对象应该根据配置匹配Date对象.我做错了什么,或者我误解了什么?我是不是从Jackson文档中读了太多关于单词automatically的内容,并且产生了字符串表示法(尽管不是ISO 8601)这一事实正在产生广告中的自动功能?