Jackson automatic formatting of Joda DateTime to ISO 8601 format Jackson automatic formatting of Joda DateTime to ISO 8601 format json json

Jackson automatic formatting of Joda DateTime to ISO 8601 format


I was able to get the answer to this from the Jackson user mailing list, and wanted to share with you since it is a newbie issue. From reading the Jackson Date FAQ, I did not realize that extra dependencies and registration are required, but that is the case. It is documented at the git hub project page here https://github.com/FasterXML/jackson-datatype-joda

Essentially, I had to add another dependency to a Jackson jar specific to the Joda data type, and then I had to register the use of that module on the object mapper. The code snippets are below.

For my Jackson Joda data type Maven dependency setup I used this:

<dependency>    <groupId>com.fasterxml.jackson.datatype</groupId>    <artifactId>jackson-datatype-joda</artifactId>    <version>${jackson.version}</version></dependency>

To register the Joda serialization/deserialization feature I used this:

ObjectMapper objectMapper = new ObjectMapper();objectMapper.registerModule(new JodaModule());objectMapper.configure(com.fasterxml.jackson.databind.SerializationFeature.    WRITE_DATES_AS_TIMESTAMPS , false);


Using Spring Boot.

Add to your Maven configuration...

<dependency>  <groupId>com.fasterxml.jackson.datatype</groupId>  <artifactId>jackson-datatype-joda</artifactId>  <version>2.7.5</version></dependency>

Then to your WebConfiguration...

@Configurationpublic class WebConfiguration extends WebMvcConfigurerAdapter{    public void configureMessageConverters(List<HttpMessageConverter<?>> converters)    {      final MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();      final ObjectMapper objectMapper = new ObjectMapper();      //configure Joda serialization      objectMapper.registerModule(new JodaModule());      objectMapper.configure(          com.fasterxml.jackson.databind.SerializationFeature.            WRITE_DATES_AS_TIMESTAMPS , false);      // Other options such as how to deal with nulls or identing...      objectMapper.setSerializationInclusion (         JsonInclude.Include.NON_NULL);      objectMapper.enable(SerializationFeature.INDENT_OUTPUT);      converter.setObjectMapper(objectMapper);      converters.add(converter);      super.configureMessageConverters(converters);    }}


In Spring Boot the configuration is even simpler. You just declare Maven dependency

    <dependency>        <groupId>com.fasterxml.jackson.datatype</groupId>        <artifactId>jackson-datatype-joda</artifactId>    </dependency>

and then add configuration parameter to your application.yml/properties file:

spring.jackson.serialization.write-dates-as-timestamps: false