在 Spring Boot 中使用 Jackson 反序列化 java.time.ZoneId
Deserialize java.time.ZoneId using Jackson in Spring Boot
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ss.SSS")
private LocalDateTime accessStart;
private ZoneId accessStartZoneId;
Jackson 中是否有内置模式映射器来映射包含 java ZoneId
的 JSON 请求?
{
"accessStart": "2017-10-10T10:10:10.100",
"accessStartZoneID": "Australia/Sydney"
}
我正在使用 Spring Boot 1.5.4.RELEASE。如何为 ZoneId
配置解串器?它适用于 accessStart
JSON parse error: Can not construct instance of java.time.ZoneId: abstract types either need to be mapped to concrete types, have custom deserializer, or contain additional type information
退一步
我特别想知道你为什么使用 LocalDateTime
along with ZoneId
. Wouldn't it be easier to use only ZonedDateTime
?
在设计方面,ZonedDateTime
is a combination of a LocalDateTime
and a ZoneId
。
支持ZoneId
假设您仍想使用 ZoneId
,您可以编写自己的自定义(反)序列化程序。
或者,您可以将 jackson-datatype-jsr310
工件添加到您的依赖项中:
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
<version>2.9</version>
</dependency>
然后在你的ObjectMapper
中注册JavaTimeModule
模块:
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new JavaTimeModule());
在 Spring 引导应用程序中,您需要以下内容:
@Configuration
public class JacksonConfig {
@Bean
public ObjectMapper createObjectMapper() {
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new JavaTimeModule());
mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
return mapper;
}
}
根据文档:支持
ZoneId
and ZoneOffset
, which do not actually store dates and times but are supported with this module nonetheless.
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ss.SSS")
private LocalDateTime accessStart;
private ZoneId accessStartZoneId;
Jackson 中是否有内置模式映射器来映射包含 java ZoneId
的 JSON 请求?
{
"accessStart": "2017-10-10T10:10:10.100",
"accessStartZoneID": "Australia/Sydney"
}
我正在使用 Spring Boot 1.5.4.RELEASE。如何为 ZoneId
配置解串器?它适用于 accessStart
JSON parse error: Can not construct instance of java.time.ZoneId: abstract types either need to be mapped to concrete types, have custom deserializer, or contain additional type information
退一步
我特别想知道你为什么使用 LocalDateTime
along with ZoneId
. Wouldn't it be easier to use only ZonedDateTime
?
在设计方面,ZonedDateTime
is a combination of a LocalDateTime
and a ZoneId
。
支持ZoneId
假设您仍想使用 ZoneId
,您可以编写自己的自定义(反)序列化程序。
或者,您可以将 jackson-datatype-jsr310
工件添加到您的依赖项中:
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
<version>2.9</version>
</dependency>
然后在你的ObjectMapper
中注册JavaTimeModule
模块:
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new JavaTimeModule());
在 Spring 引导应用程序中,您需要以下内容:
@Configuration
public class JacksonConfig {
@Bean
public ObjectMapper createObjectMapper() {
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new JavaTimeModule());
mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
return mapper;
}
}
根据文档:支持
ZoneId
andZoneOffset
, which do not actually store dates and times but are supported with this module nonetheless.