Closed yuzmann closed 1 year ago
I cannot reproduce the issue: use of @JsonFormat
annotation and global format configuration work.
In case of custom serializer, however, you must implement all support for annotations (and global settings) yourself. Jackson has very little control over custom implementations and so it cannot force specific format.
Note that for configuring per-type format defaults, you need to use "config overrides" like so:
ObjectMapper m = mapperBuilder().withConfigOverride(OffsetDateTime.class,
cfg -> cfg.setFormat(JsonFormat.Value.forPattern("yyyy.MM.dd'x'HH:mm:ss")))
.build();
At this point I would need a unit test to reproduce the problem.
Attached is a sample spring boot project.
HealthCheckControllerIT test class can reproduce the issue.
Unfortunately I cannot use tests that rely on external frameworks: a stand-alone example would be needed. This is due to 2 issues: first, frameworks often do their own initialization or special configuration; and second, I can't add unit tests that would require dependencies to external frameworks.
Cannot reproduce (no stand-alone reproduction), appear to work, closing.
May be re-opened with a stand-alone reproduction against 2.14 or 2.15 branch.
After configuring ObjectMapper globally and with annotation, serialization works expectedly on Date and LocalDateTime types.
As for OffsetDateTime, @JsonFormat annotation doesn't work and mapper always use Global config.
In order to solve the issue, I need to use custom serializer and @JsonSerialize annotation as below,
@JsonSerialize(using = OffsetDateTimeSerializerImpl.class) private OffsetDateTime offsetDateTimeAnnotated;
Note: JsonMapper has the same problem with ObjectMapper
Maven:
Formatters:
For Global ('G' between date and time) Date: yyyy-MM-dd'G'HH:mm:ss LocalDateTime: yyyy-MM-dd'G'HH:mm:ss.SSS OffsetDateTime: yyyy-MM-dd'G'HH:mm:ssZ
For Annotation ('A' between date and time) Date: yyyy-MM-dd'A'HH:mm:ss LocalDateTime: yyyy-MM-dd'A'HH:mm:ss.SSS OffsetDateTime: yyyy-MM-dd'A'HH:mm:ssZ
Result: { "date" : "2022-12-02G17:29:02", "dateAnnotated" : "2022-12-02A17:29:02", "localDateTime" : "2022-12-02G17:29:02.194", "localDateTimeAnnotated" : "2022-12-02A17:29:02.194", "offsetDateTime" : "2022-12-02G17:29:02+0300", "offsetDateTimeAnnotated" : "2022-12-02G17:29:02+0300" <-- It must be in format: "yyyy-MM-dd'A'HH:mm:ssZ" }