2

我在java.sql.Timestamp使用 Jackson 2.8.5 解析 XML 文件时遇到问题。不知何故,毫秒在左侧用零填充。

这是一个最小的示例,表明:

public class Foo {

    @JacksonXmlProperty(localName = "ts")
    Timestamp ts;

    public static void main(String[] args) throws IOException {
        String xml = "<Foo><ts>2017-09-21T11:25:32.1Z</ts></Foo>"
        Foo foo = new XmlMapper().readValue(xml, Foo.class);
        System.out.println("foo.ts = " + foo.ts);
    }
}

foo.ts = 2017-09-21 11:25:32.001

而如果我手动解析字符串,我会得到预期值

System.out.println(Instant.parse("2017-09-21T11:25:32.1Z"));

2017-09-21 11:25:32.1

4

1 回答 1

2

如this answer中所述,这似乎是一个问题SimpleDateFormat(杰克逊在内部使用)。我还使用纯 Java(没有 Jackson)进行了测试,并且错误也发生了:

String s = "2017-09-21T11:25:32.1Z";
Date date = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SX").parse(s);
System.out.println(new Timestamp(date.getTime())); // 2017-09-21 08:25:32.001

如果您可以更改 xml,则向输入 ( 2017-09-21T11:25:32.100Z) 添加零即可。


另一种选择是为您的字段编写自定义反序列化器(使用Timestamp类中的正确方法从 转换Instant):

public class CustomTimestampDeserializer extends JsonDeserializer<Timestamp> {

    @Override
    public Timestamp deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {
        return Timestamp.from(Instant.parse(p.getText()));
    }
}

然后您注释该字段以使用此自定义反序列化器:

@JacksonXmlProperty(localName = "ts")
@JsonDeserialize(using = CustomTimestampDeserializer.class)
Timestamp ts;

现在,在打印 时Timestamp,您会得到正确的值:

2017-09-21 08:25:32.1

等一下,为什么是小时08- 那是因为当你System.out.printlna时Timestamp,它隐式调用该toString()方法,并且此方法将 转换Timestamp为 JVM 默认时区(在我的情况下,它是America/Sao_Paulo,所以它是正确的,因为圣保罗的 08:25 AM 相当于 11:25 AM世界标准时间)。但是保留的值Timestamp是正确的。

toString()你可以在这篇文章中阅读更多关于这种行为的信息——它谈到了java.util.Date,但想法是一样的(特别是因为Timestampextends Date,所以它有同样的问题)。


要将其序列化回 xml,您还可以配置自定义序列化程序:

public class CustomTimestampSerializer extends JsonSerializer<Timestamp> {

    @Override
    public void serialize(Timestamp value, JsonGenerator gen, SerializerProvider serializers) throws IOException, JsonProcessingException {
        gen.writeString(value.toInstant().toString());
    }
}

然后您注释该字段以使用此序列化程序:

@JacksonXmlProperty(localName = "ts")
@JsonDeserialize(using = CustomTimestampDeserializer.class)
@JsonSerialize(using = CustomTimestampSerializer.class)
Timestamp ts;

只是一个细节:Instant.toString()将导致2017-09-21T11:25:32.100Z. 如果您最后不想要那些额外的零,您可以使用DateTimeFormatter. 所以自定义序列化器将是这样的:

public class CustomTimestampSerializer extends JsonSerializer<Timestamp> {

    private DateTimeFormatter fmt = new DateTimeFormatterBuilder()
        // date/time
        .appendPattern("yyyy-MM-dd'T'HH:mm:ss")
        // nanoseconds without leading zeroes (from 0 to 9 digits)
        .appendFraction(ChronoField.NANO_OF_SECOND, 0, 9, true)
        // offset (Z)
        .appendOffsetId()
        // create formatter (set zone to UTC to properly format the Instant)
        .toFormatter().withZone(ZoneOffset.UTC);

    @Override
    public void serialize(Timestamp value, JsonGenerator gen, SerializerProvider serializers) throws IOException, JsonProcessingException {
        gen.writeString(fmt.format(value.toInstant()));
    }
}

这会将时间戳打印为2017-09-21T11:25:32.1Z.

于 2017-09-26T13:14:42.207 回答