LocalDateTime-使用LocalDateTime.parse反序列化

LocalDateTime-使用LocalDateTime.parse反序列化,第1张

LocalDateTime-使用LocalDateTime.parse反序列化

香草杰克逊没有一种方法来 反序列化 一个

LocalDateTime
从任何JSON字符串值对象。

您有几种选择。您可以创建和注册自己的

JsonDeserializer
使用方法
LocalDateTime#parse

class ParseDeserializer extends StdDeserializer<LocalDateTime> {    public ParseDeserializer() {        super(LocalDateTime.class);    }    @Override    public LocalDateTime deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {        return LocalDateTime.parse(p.getValueAsString()); // or overloaded with an appropriate format    }}...@JsonSerialize(using = ToStringSerializer.class)@JsonDeserialize(using = ParseDeserializer.class)private LocalDateTime initiationDate;

或者你也可以添加杰克逊的

java.time
延伸到类路径中并注册相应的
Module
与你的
ObjectMapper

objectMapper.registerModule(new JavaTimeModule());

让杰克逊为您做转换。在内部,这

LocalDateTime#parse
与一种标准格式一起使用。幸运的是,它支持像

2016-05-11T17:32:20.897

盒子外面。



欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5620177.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-15
下一篇 2022-12-15

发表评论

登录后才能评论

评论列表(0条)

保存