我编写了一个自定义反序列化器,以秒为单位处理时间戳(Groovy语法)。
class UnixTimestampDeserializer extends JsonDeserializer<DateTime> { Logger logger = LoggerFactory.getLogger(UnixTimestampDeserializer.class) @Override DateTime deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException { String timestamp = jp.getText().trim() try { return new DateTime(Long.valueOf(timestamp + '000')) } catch (NumberFormatException e) { logger.warn('Unable to deserialize timestamp: ' + timestamp, e) return null } }}
class TimestampThing { @JsonDeserialize(using = UnixTimestampDeserializer.class) DateTime timestamp @JsonCreator public TimestampThing(@JsonProperty('timestamp') DateTime timestamp) { this.timestamp = timestamp }}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)