使用 "ObjectMapper" 格式化 JSON 数据
Formatting JSON data with "ObjectMapper"
我有一个字段:
@NotNull
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd")
private LocalDate date;
但是当我获取 JSON 格式的数据时 - 它是另一个:
"date":{"year":2020,"month":"JANUARY","monthValue":1,"dayOfMonth":6,"dayOfWeek":"MONDAY","dayOfYear":6,"era":"CE","chronology":{"calendarType":"iso8601","id":"ISO"},"leapYear":true}
如何解决?
您可以为它编写自定义 serializer/deserializer。
@JsonDeserialize(using = LocalDateDeserializer.class)
@JsonSerialize(using = LocalDateSerializer.class)
private LocalDate date;
本文更详细地介绍了如何创建这些 类 - https://kodejava.org/how-to-format-localdate-object-using-jackson/
我有一个字段:
@NotNull
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd")
private LocalDate date;
但是当我获取 JSON 格式的数据时 - 它是另一个:
"date":{"year":2020,"month":"JANUARY","monthValue":1,"dayOfMonth":6,"dayOfWeek":"MONDAY","dayOfYear":6,"era":"CE","chronology":{"calendarType":"iso8601","id":"ISO"},"leapYear":true}
如何解决?
您可以为它编写自定义 serializer/deserializer。
@JsonDeserialize(using = LocalDateDeserializer.class)
@JsonSerialize(using = LocalDateSerializer.class)
private LocalDate date;
本文更详细地介绍了如何创建这些 类 - https://kodejava.org/how-to-format-localdate-object-using-jackson/