Klaus Stadler
Klaus Stadler

Reputation: 405

Disabling timezone adjustments of jackson in a spring-integration application

I'm deserialzing JSON in a spring integration application like this:

<int:json-to-object-transformer type="com.something.domain.Transaction[]" />

This is generally working well, apart from handling dates.

The annotaded class contains the following:

@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ss")
@JsonProperty("clickDate")
private Date clickDate;

The date is parsed without error, but testing revealed that jackson does make timezone adjustments. E.g. if the JSON contains the date "2018-02-15T11:43:00", the deserializer converts it to the equivalent of "2018-02-15T12:43:00" (one hour difference).

Is it possible to disable these adjustments, so that the original date is deserialized?

Upvotes: 2

Views: 4644

Answers (2)

Manos Nikolaidis
Manos Nikolaidis

Reputation: 22264

Jackson will by default parse a Date using UTC timezone which will not modify the time. Confirm that Jackson's timezone hasn't been modified. E.g. if mapper is the ObjectMapper

System.out.println(mapper.getDeserializationConfig().getTimeZone());

you can then set the timezone with setTimeZone

If it proves impossible to modify Jackson's timezome settings you can override them for a field by specifying a timezone in @JsonFormat annotation:

@JsonFormat(shape = JsonFormat.Shape.STRING,
            pattern = "yyyy-MM-dd'T'HH:mm:ss",
            timezone = "UTC")
Date clickDate;

Upvotes: 0

Artem Bilan
Artem Bilan

Reputation: 121590

Not sure what you mean so far about disabling, but if it is even possible, you can do that via:

<xsd:attribute name="object-mapper" use="optional">
        <xsd:annotation>
            <xsd:documentation>
                Optional reference to a JsonObjectMapper instance.
                By default, a JsonObjectMapper that uses a Jackson 2 ObjectMapper.
            </xsd:documentation>
            <xsd:appinfo>
                <tool:annotation kind="ref">
                    <tool:expected-type type="org.springframework.integration.support.json.JsonObjectMapper" />
                </tool:annotation>
            </xsd:appinfo>
        </xsd:annotation>
    </xsd:attribute>

and use:

public Jackson2JsonObjectMapper(ObjectMapper objectMapper) {

with the customized ObjectMapper from Jackson.

By default Spring Integration uses this one:

public Jackson2JsonObjectMapper() {
    this.objectMapper = new ObjectMapper();
    this.objectMapper.configure(MapperFeature.DEFAULT_VIEW_INCLUSION, false);
    this.objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    registerWellKnownModulesIfAvailable();
}

where that registerWellKnownModulesIfAvailable() stands for these modules:

com.fasterxml.jackson.datatype.jdk7.Jdk7Module
com.fasterxml.jackson.datatype.jdk8.Jdk8Module
com.fasterxml.jackson.datatype.jsr310.JavaTimeModule
com.fasterxml.jackson.datatype.joda.JodaModule
com.fasterxml.jackson.module.kotlin.KotlinModule

So, maybe even one of those time-based module can help you without any modifications.

Upvotes: 1

Related Questions