Search code examples
spring-bootjacksonspring-integrationobjectmapperjsr310

How to set date format for JsonObjectMapper in Spring Integration


I am converting my Java object to Map using Spring Integration ObjectToMapTransformer's transformPayload().

Everything works fine except that the Instant fields in my object are getting broken into epochSecond and nano, which in turn throws exception while persisting in data-store (MongoDB).

This is the Spring Integration JsonObjectMapper being used to convert the Object to Map: private final JsonObjectMapper<?, ?> jsonObjectMapper = JsonObjectMapperProvider.newInstance();

My question is how can I configure date format for the above mapper. Just like Jackson's ObjectMapper::configure(), do we have any similar options here? I cannot find any, neither in source code nor on internet!!

I also tried enabling/disabling spring.jackson.serialization.WRITE_DATES_AS_TIMESTAMPS in my application.properties, but no joy!

I have jackson-datatype-jsr310 dependency in my pom.xml

How to get the Instant in correct format?


Solution

  • I think we should add support for custom JsonObjectMapper injection. That way you would be able to build Jackson2JsonObjectMapper based on desired ObjectMapper.

    Please, raise a JIRA ticket on the matter and don't hesitate with the contribution: https://github.com/spring-projects/spring-integration/blob/master/CONTRIBUTING.adoc

    Meanwhile as a workaround I'd suggest a pair of ObjectToJsonTransformer/JsonToObjectTransformer:

    .transform(Transformers.toJson(jsonObjectMapper(), ObjectToJsonTransformer.ResultType.NODE))
    .transform(Transformers.fromJson(Map.class, jsonObjectMapper()))