【发布时间】:2016-04-25 23:28:08
【问题描述】:
我正在使用带有 Jackson 的 Jersey 作为 JSON 提供程序。我可以将ZonedDateTime 序列化为 JSON,但是当我想反序列化时,它会给我如下错误。
能否请您帮我说出完成此反序列化工作所需的确切配置。
原因:com.fasterxml.jackson.databind.JsonMappingException:无法从字符串值('2016-01-21T21:00:00Z')实例化类型[简单类型,类 java.time.ZonedDateTime] 的值;没有单字符串构造函数/工厂方法
我的mapper配置如下:
@Provider
public class ObjectMapperContextResolver implements ContextResolver<ObjectMapper> {
private final ObjectMapper MAPPER;
public ObjectMapperContextResolver() {
MAPPER = new ObjectMapper();
//This would add JSR310 (Datetime) support while converting date to JSON using JAXRS service
MAPPER.registerModule(new JavaTimeModule());
//Below line would disable use of timestamps (numbers),
//and instead use a [ISO-8601 ]-compliant notation, which gets output as something like: "1970-01-01T00:00:00.000+0000".
MAPPER.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
}
@Override
public ObjectMapper getContext(Class<?> type) {
return MAPPER;
}
}
【问题讨论】: