admin管理员组文章数量:1122832
I would like to make a REST call to a partner system, with Json object in the body. We use ZonedDateTime, and I need to send the dates in the format "2025-01-05T21:41:24.195Z".
Currently it is serialized to the format "2025-01-03T12:46:09.00688+01:00".
I have the following configuration:
@Bean
public ObjectMapper objectMapper() {
ObjectMapper objectMapper = new ObjectMapper();
JavaTimeModule javaTimeModule = new JavaTimeModule();
javaTimeModule.addSerializer(ZonedDateTime.class, new ZonedDateTimeSerializer(DateTimeFormatter.ISO_INSTANT));
objectMapper.registerModule(javaTimeModule);
objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
return objectMapper;
}
I inject this objectMapper into my service and expected this to serialize the date in the correct format, but it is not enough.
I would like to make a REST call to a partner system, with Json object in the body. We use ZonedDateTime, and I need to send the dates in the format "2025-01-05T21:41:24.195Z".
Currently it is serialized to the format "2025-01-03T12:46:09.00688+01:00".
I have the following configuration:
@Bean
public ObjectMapper objectMapper() {
ObjectMapper objectMapper = new ObjectMapper();
JavaTimeModule javaTimeModule = new JavaTimeModule();
javaTimeModule.addSerializer(ZonedDateTime.class, new ZonedDateTimeSerializer(DateTimeFormatter.ISO_INSTANT));
objectMapper.registerModule(javaTimeModule);
objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
return objectMapper;
}
I inject this objectMapper into my service and expected this to serialize the date in the correct format, but it is not enough.
Share Improve this question asked yesterday RekaKRekaK 1131 silver badge11 bronze badges 17 | Show 12 more comments1 Answer
Reset to default 1This approach works:
@Test
void test() throws Exception {
var formatter = new DateTimeFormatterBuilder()
.parseCaseInsensitive()
.appendInstant(3)
.toFormatter();
var serializer = new ZonedDateTimeSerializer(formatter);
var javaTimeModule = new JavaTimeModule();
javaTimeModule.addSerializer(ZonedDateTime.class, serializer);
var objectMapper = new ObjectMapper();
objectMapper.registerModule(javaTimeModule);
objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
var str = objectMapper.writeValueAsString(ZonedDateTime.now());
System.out.println(str);
}
The test prints
"2025-01-06T16:32:37.855Z"
I think you should make sure the mapper injected into your target bean is the one you are configuring and not e.g. default mapper created by Jackson. Try this:
@Bean
ObjectMapper jacksonObjectMapper(Jackson2ObjectMapperBuilder builder) {
var formatter = new DateTimeFormatterBuilder()
.parseCaseInsensitive()
.appendInstant(3)
.toFormatter();
var serializer = new ZonedDateTimeSerializer(formatter);
var javaTimeModule = new JavaTimeModule();
javaTimeModule.addSerializer(ZonedDateTime.class, serializer);
return builder
.modules(javaTimeModule)
.featuresToDisable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
.build();
}
Pay attention to method (bean) name.
P.S. There's JacksonAutoConfiguration.JacksonObjectMapperConfiguration
providing the declaration of ObjectMapper
:
@Configuration(proxyBeanMethods = false)
@ConditionalOnClass(Jackson2ObjectMapperBuilder.class)
static class JacksonObjectMapperConfiguration {
@Bean
@Primary
@ConditionalOnMissingBean
ObjectMapper jacksonObjectMapper(Jackson2ObjectMapperBuilder builder) {
return builder.createXmlMapper(false).build();
}
}
This is where the default mapper is created. To override it you need to declare your own ObjectMapper with the same name and type which will be used for HTTP response serialization.
本文标签: javaSerialize ZonedDateTime in specific formatStack Overflow
版权声明:本文标题:java - Serialize ZonedDateTime in specific format - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736281072a1926201.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
Instant
– g00se Commented yesterday