【发布时间】:2015-04-22 15:57:08
【问题描述】:
我的 JSON 输出如下所示:
"company":{"companyId": 0, "companyName": "OnTarget Technologies", "companyTypeId": 0, "address": null,…},
"projectParentId": 24,
"projectAddress":{"addressId": 26, "address1": "4750 59th street", "address2": "Apt 9C", "city": "Woodside",…},
"taskList":[{"projectTaskId": 9, "title": "Installation of Lights", "description": "Installation of lights",…],
"projects": null,
"startDate": 1424322000000,
"endDate": 1427515200000,
"projectImagePath": null
},
{"projectId": 26, "projectName"
数据库中startDate和endDate的数据类型是Datetime
我在 json 中作为长整数序列化时获取日期时间。
如何在序列化时将其转换为可读格式 格式 MM-dd-yyyy HH:mm:ss
我创建了一个提供程序,但它不起作用
这是我的提供者:
@Component
@Provider
public class MyObjectMapper implements ContextResolver<ObjectMapper> {
private final ObjectMapper mapper;
public MyObjectMapper() {
this.mapper = createObjectMapper();
}
private static ObjectMapper createObjectMapper() {
ObjectMapper mapper = new ObjectMapper();
mapper.configure(SerializationConfig.Feature.WRITE_DATES_AS_TIMESTAMPS, true);
mapper.setDateFormat(new SimpleDateFormat("MM-dd-yyyy HH:mm:ss"));
mapper.configure(SerializationConfig.Feature.INDENT_OUTPUT, true);
return mapper;
}
@Override
public ObjectMapper getContext(Class<?> aClass) {
return this.mapper;
}
}
我正在使用jacskon jersey 2.x,spring mysql数据库
任何想法都值得赞赏。
谢谢 桑吉夫
【问题讨论】:
-
你用的是哪个版本的jackson?
-
怎么样:从对象映射中获取长值,但在插入数据库之前转换为日期
-
我使用的是杰克逊 2.4.3。
-
@EfeKahraman 这是在将 JSON 发送回客户端期间。