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
  • 2 Show exactly how you are injecting/using the mapper. The mapper itself is serializing as per your requirement. I suspect you are injecting and using springs mapper, not the one you defined. – Chaosfire Commented yesterday
  • 1 And the fields are ZonedDateTime? I only see an OffsetDateTime, the ZoneId (country) missing. – Joop Eggen Commented yesterday
  • 2 Well we need clarification. We get ... but there is the ZoneId in the object. If the only zone id that's going to be used is Z then it might as well be Instant – g00se Commented yesterday
  • 2 Thanks for answering all the follow-up questions from the comments. Please add the information as edits to the question itself so we have everything in one place. Don’t expect readers to dig through all the comments to understand the question. Most readers won’t. – Anonymous Commented yesterday
  • 2 And if there’s any way you can: A minimal reproducible example, please? – Anonymous Commented yesterday
 |  Show 12 more comments

1 Answer 1

Reset to default 1

This 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