我编写了一个自定义反序列化器,以秒为单位处理时间戳(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 } }}然后,我注释了POGO以将其用于时间戳记:
class TimestampThing { @JsonDeserialize(using = UnixTimestampDeserializer.class) DateTime timestamp @JsonCreator public TimestampThing(@JsonProperty('timestamp') DateTime timestamp) { this.timestamp = timestamp }}


