Serialize JSON date format from ZonedDateTime to custom date format using ObjectMapper

Viewed 1958

ObjectMapper doesn't format ZonedDateTime object to customized one.

POJO is not under my control so that I can't change it. I need to serialize POJO object for WS. POJO has ZonedDateTime (I don't know why because it's date is from database).

I am using Spring-boot 2.1.8.RELEASE, so... I put this into my dependencies:

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

I also added this in application.properties:

spring.jackson.serialization.WRITE_DATES_AS_TIMESTAMPS=false

In configuration file, I added this bean in configuration file because it is important that configure ObjectMapper as soon as possible to accept changes:

@Bean
public ObjectMapper objectMapper() {
    ObjectMapper mapper = new ObjectMapper();
    mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
    mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
    mapper.setTimeZone(TimeZone.getTimeZone(Locale.FRANCE.getDisplayName()));
    JavaTimeModule javaTimeModule = new JavaTimeModule();
    javaTimeModule.addSerializer(ZonedDateTime.class, new ZonedDateTimeSerializer());
    mapper.registerModule(javaTimeModule);
    return mapper;
}

And this class:

public class ZonedDateTimeSerializer extends JsonSerializer {

public static final DateTimeFormatter FORMATTER = 
DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ssZ", Locale.FRANCE);
@Override
public void serialize(Object value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
    gen.writeString(((ZonedDateTime)value).format(FORMATTER));
}

I have POJO with ZonedDateTime field:

public ZonedDateTime getStartDate() {
    return this.startDate != null ? this.startDate.withNano(0).withZoneSameLocal(ZoneId.of("Europe/Paris")) : null;
}

public void setStartDate(ZonedDateTime startDate) {
    this.startDate = startDate != null ? startDate.withNano(0).withZoneSameLocal(ZoneId.of("Europe/Paris")) : null;
}

And in my code I autowired object mapper and serielize this POJO like this:

private final ObjectMapper mapper;
public MyClass(ObjectMapper mapper) {
    this.mapper = mapper;
}
...
mapper.writeValueAsString(contactVersion);

But what I got is as follows:

"startDate":{"offset":{"totalSeconds":7200,"id":"+02:00","rules":{"transitions":[],...

and a lot of information, 82.634765625kb information for that time and all I want is:

"2019-10-15T17:00:53Z"

SOLVED: it just works. I use IntelliJ, and I deploy under Weblogic 12.2.1.3.0 as autodeploy and it could be that I didn't run Maven clean so I just run Maven install and run it in debug mode so it was started without WRITE_DATE_AS_TIMESTAMPS=false property with old application.properties file. I really don't know, but it works and it should be like this.

1 Answers

Create a getter with a return type string and mark it as a property (and mark your original getter or property as @JsonIgnored). Just like that:

@JsonGetter("startDate")
public String getStartDateAsString() {
    return DateTimeFormatter.ISO_LOCAL_DATE_TIME.format(startDate);
}
Related