我正试图让Jackson解析我无法更改的特定API给我的日期时间值。
所以我给它一个自定义模式:
public class Problem {
public static void main(String[] args) throws Exception {
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new JavaTimeModule());
String json = "{\"posted_at\":\"Mon, 14 Oct 2019 13:00:00 +0900\"}";
Model model = mapper.readValue(json, Model.class);
System.out.println(model.posted_at);
}
public static class Model {
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "E, d MMM yyyy HH:mm:ss XX")
public ZonedDateTime posted_at;
}
}
我收到此异常:
Exception in thread "main" java.time.format.DateTimeParseException: Text 'Mon, 14 Oct 2019 13:00:00 +0900' could not be parsed at index 0
似乎在索引0处失败,但是我不明白为什么,因为对于星期几格式模式“ E”,似乎“ Mon”应该是正确的值。
到目前为止已尝试:
设置DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE
:
mapper.configure(DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE, false)
结果:相同的例外。
添加Jdk8Module
:
ObjectMapper mapper = new ObjectMapper()
.registerModule(new Jdk8Module())
.registerModule(new JavaTimeModule());
结果:相同的例外。
答案 0 :(得分:2)
Jackson在Joda DateTime上已经使用了很长时间,并且从您的错误消息看来,您正在使用Java.DateTime添加对Java dateTime的支持,您需要添加一些额外的依赖项。“ jackson-datatype-jdk8” < / strong>参见https://github.com/FasterXML/jackson-modules-java8
// run MP3::play_mp3 on object mp3
std::thread second(&MP3::play_mp3, mp3,
"/home/robodyne/Project/audio/scegli-rifiuto.mp3");
// ...
second.join();
然后您设置 ADJUST_DATES_TO_CONTEXT_TIME_ZONE
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jdk8</artifactId>
</dependency>
然后您添加新模块
ObjectMapper mapper = new ObjectMapper();
mapper.configure(DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE, false)