Java LocalDateTime - 使用 LocalDateTime.parse 进行反序列化
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37166217/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
LocalDateTime - deserialization with LocalDateTime.parse
提问by Andrew Tobilko
I have the field initiationDatewhich serialises by ToStringSerializerclass to ISO-8601 format.
我有initiationDate按ToStringSerializer类序列化为ISO-8601 格式的字段。
@JsonSerialize(using = ToStringSerializer.class)
private LocalDateTime initiationDate;
When I receive the following JSON,
当我收到以下 JSON 时,
...
"initiationDate": "2016-05-11T17:32:20.897",
...
I want to deserialize it by LocalDateTime.parse(CharSequence text)factory method. All my attempts ended with com.fasterxml.Hymanson.databind.JsonMappingException:
我想通过LocalDateTime.parse(CharSequence text)工厂方法反序列化它。我所有的尝试都以com.fasterxml.Hymanson.databind.JsonMappingException:
Can not instantiate value of type [simple type, class
java.time.LocalDateTime] fromStringvalue ('2016-05-11T17:32:20.897'); no single-Stringconstructor/factory method
无法
java.time.LocalDateTime从String值 ('2016-05-11T17:32:20.897') 中实例化 [简单类型,类]类型的值;没有单一的String构造函数/工厂方法
How do I achieve that? How can I specify factory method?
我如何做到这一点?如何指定工厂方法?
EDIT:
编辑:
The problem has been solved by including Hymanson-datatype-jsr310 moduleto the project and using @JsonDeserializewith LocalDateTimeDeserializer.
通过在项目中包含 Hymanson-datatype-jsr310 模块并@JsonDeserialize与LocalDateTimeDeserializer.
@JsonSerialize(using = ToStringSerializer.class)
@JsonDeserialize(using = LocalDateTimeDeserializer.class)
private LocalDateTime initiationDate;
采纳答案by Sotirios Delimanolis
Vanilla Hymanson doesn't have a way to deserializea LocalDateTimeobject from any JSON string value.
香草Hyman逊没有一种方法来反序列化一个LocalDateTime从任何JSON字符串值对象。
You have a few options. You can create and register your own JsonDeserializerwhich will use LocalDateTime#parse.
你有几个选择。您可以创建和注册自己的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;
Or you can add Hymanson's java.timeextensionto your classpath and register the appropriate Modulewith your ObjectMapper.
或者您可以将Hymanson 的java.time扩展添加到您的类路径中,并在Module您的ObjectMapper.
objectMapper.registerModule(new JavaTimeModule());
and let Hymanson do the conversion for you. Internally, this uses LocalDateTime#parsewith one of the standard formats. Fortunately, it supports values like
让Hyman逊为你做转换。在内部,这LocalDateTime#parse与标准格式之一一起使用。幸运的是,它支持这样的值
2016-05-11T17:32:20.897
out of the box.
盒子外面。
回答by Yan Khonski
For those who want to parse custom date-time format.
对于那些想要解析自定义日期时间格式的人。
1) Add dependency
1) 添加依赖
compile "com.fasterxml.Hymanson.datatype:Hymanson-datatype-jsr310:2.8.8"
2) Json annotation with date-time format
2) 带有日期时间格式的 Json 注解
public class ClientRestObject {
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private LocalDateTime timestamp;
}
3) Register Java8 module in ObjectMapper
3)在ObjectMapper中注册Java8模块
private static ObjectMapper buildObjectMapper() {
ObjectMapper objectMapper = new ObjectMapper();
//To parse LocalDateTime
objectMapper.registerModule(new JavaTimeModule());
return objectMapper;
}

