Wenn aus irgendeinem Grund jackson-modules-java8
nicht verwendet werden kann, können Sie das Instant-Feld als long
(de-)serialisieren, indem Sie @JsonIgnore
und @JsonGetter
& @JsonSetter
verwenden:
public class MyBean {
private Instant time = Instant.now();
@JsonIgnore
public Instant getTime() {
return this.time;
}
public void setTime(Instant time) {
this.time = time;
}
@JsonGetter
private long getEpochTime() {
return this.time.toEpochMilli();
}
@JsonSetter
private void setEpochTime(long time) {
this.time = Instant.ofEpochMilli(time);
}
}
Beispiel:
@Test
public void testJsonTime() throws Exception {
String json = new ObjectMapper().writeValueAsString(new MyBean());
System.out.println(json);
MyBean myBean = new ObjectMapper().readValue(json, MyBean.class);
System.out.println(myBean.getTime());
}
ergibt
{"epochTime":1506432517242}
2017-09-26T13:28:37.242Z