I have a class that will get serialized into JSON with the following attribute:
@JsonProperty("validTo")
@org.springframework.format.annotation.DateTimeFormat(iso = org.springframework.format.annotation.DateTimeFormat.ISO.DATE_TIME)
private OffsetDateTime validTo;
I'm also using the default spring-boot ObjectMapper with Jackson. This works fine if the validTo
DateTime has non-zero mili/nanoseconds. However, if they are zero, the result will not include them at all.
Value OffsetDateTime.of(2000, 10, 10, 10, 10, 10, 1000000, ZoneOffset.UTC)
would correctly translate to 2000-10-10T10:10:10.001Z
But OffsetDateTime.of(2000, 10, 10, 10, 10, 10, 0, ZoneOffset.UTC)
would incorrectly translate to 2000-10-10T10:10:10Z
Why is that?
The javadoc of the format used in the annotation describes it as The most common ISO Date Time Format yyyy-MM-dd'T'HH:mm:ss.SSSXXX — for example, "2000-10-31T01:30:00.000-05:00".
, which appears to include the zeros.
It looks like Jackson ignores
@DateTimeFormat
annotation and the real serialization happens incom.fasterxml.jackson.datatype.jsr310.ser.OffsetDateTimeSerializer
.You can test it if you annotate the field like this (it won't affect the result):