LocalDateTime

编程入门 行业动态 更新时间:2024-10-11 17:28:36
本文介绍了LocalDateTime - 使用LocalDateTime.parse进行反序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有字段 initiationDate ,它按 ToStringSerializer 类序列化为ISO-8601格式。

I have the field initiationDate which serialises by ToStringSerializer class to ISO-8601 format.

@JsonSerialize(using = ToStringSerializer.class) private LocalDateTime initiationDate;

当我收到以下JSON时,

When I receive the following JSON,

... "initiationDate": "2016-05-11T17:32:20.897", ...

我想通过 LocalDateTime.parse(CharSequence text)工厂方法对其进行反序列化。我的所有尝试都以 com.fasterxml.jackson.databind.JsonMappingException 结束:

I want to deserialize it by LocalDateTime.parse(CharSequence text) factory method. All my attempts ended with com.fasterxml.jackson.databind.JsonMappingException:

无法从 String value(<$ c)实例化类型[simple type,class java.time.LocalDateTime ]的值$ C> '2016-05-11T17:32:20.897');没有单一 - 字符串构造函数/工厂方法

Can not instantiate value of type [simple type, class java.time.LocalDateTime] from String value ('2016-05-11T17:32:20.897'); no single-String constructor/factory method

我如何实现这一目标?如何指定工厂方法?

How do I achieve that? How can I specify factory method?

编辑:

通过将 jackson-datatype-jsr310模块包含在项目中并使用 @JsonDeserialize with LocalDateTimeDeserializer 。

The problem has been solved by including jackson-datatype-jsr310 module to the project and using @JsonDeserialize with LocalDateTimeDeserializer.

@JsonSerialize(using = ToStringSerializer.class) @JsonDeserialize(using = LocalDateTimeDeserializer.class) private LocalDateTime initiationDate;

推荐答案

Vanilla Jackson没办法从任何JSON字符串值反序列化 LocalDateTime 对象。

Vanilla Jackson doesn't have a way to deserialize a LocalDateTime object from any JSON string value.

您有几个选项。您可以创建并注册自己的 JsonDeserializer ,它将使用 LocalDateTime#parse 。

You have a few options. You can create and register your own JsonDeserializer which will use 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 扩展名到您的类路径并注册相应的模块使用 ObjectMapper 。

Or you can add Jackson's java.time extension to your classpath and register the appropriate Module with your ObjectMapper.

objectMapper.registerModule(new JavaTimeModule());

让杰克逊为你做转换。在内部,它使用其中一种标准格式的 LocalDateTime#parse 。幸运的是,它支持的值如

and let Jackson do the conversion for you. Internally, this uses LocalDateTime#parse with one of the standard formats. Fortunately, it supports values like

2016-05-11T17:32:20.897

开箱即用。

更多推荐

LocalDateTime

本文发布于:2023-10-29 00:56:47,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1538233.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:LocalDateTime

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!