【发布时间】:2022-01-08 02:09:10
【问题描述】:
我有带有标签<Birthday>1994-11-30</Birthday>的xml
我需要在utc 中将其转换为带有timestamp 日期的json。我的代码:
fun convertXmlToJson(profile: NotifyGetDocsRqType): JsonNode? {
return objectMapper.valueToTree(profile.data)
}
Birthday 类中的字段NotifyGetDocsRqType
@XmlElement(name = "Birthday")
protected XMLGregorianCalendar birthday;
因此,我得到了带有生日 = 786142800000 的 json。通过 utc,此值为 29 November 1994, 21:00:00(在此处查看 https://www.epochconverter.com)
objectMapper 的配置:
@Bean
fun objectMapper() = ObjectMapper()
.apply {
disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)
enable(DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_AS_NULL)
enable(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT)
enable(MapperFeature.ACCEPT_CASE_INSENSITIVE_ENUMS)
registerKotlinModule()
setTimeZone(TimeZone.getTimeZone("UTC"))
registerModule(JavaTimeModule())
registerModule(Jdk8Module())
setVisibility(PropertyAccessor.IS_GETTER, JsonAutoDetect.Visibility.NONE)
}
为什么我的代码不起作用?虽然我为objectMapper写了setTimeZone(TimeZone.getTimeZone("UTC"))
【问题讨论】:
标签: kotlin serialization timestamp utc objectmapper