代码之家  ›  专栏  ›  技术社区  ›  Klaus Stadler

在spring集成应用程序中禁用jackson的时区调整

  •  2
  • Klaus Stadler  · 技术社区  · 7 年前

    我正在spring集成应用程序中对JSON进行反序列化,如下所示:

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

    除了处理日期外,这通常效果良好。

    注释类包含以下内容:

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

    对日期的解析没有错误,但测试显示jackson确实进行了时区调整。E、 g.如果JSON包含日期“2018-02-15T11:43:00”,反序列化器将其转换为等效的“2018-02-15T12:43:00”(一小时差)。

    是否可以禁用这些调整,以便反序列化原始日期?

    2 回复  |  直到 7 年前
        1
  •  1
  •   Artem Bilan    7 年前

    不知道你到底是什么意思 正在禁用 ,但如果可能,您可以通过以下方式实现:

    <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>
    

    和使用:

    public Jackson2JsonObjectMapper(ObjectMapper objectMapper) {
    

    使用定制的 ObjectMapper 来自杰克逊。

    默认情况下,Spring集成使用以下选项:

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

    在哪里 registerWellKnownModulesIfAvailable() 代表以下模块:

    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
    

    所以,也许即使其中一个基于时间的模块也可以帮助您,而无需任何修改。

        2
  •  0
  •   Manos Nikolaidis Airsource Ltd    7 年前

    Jackson将默认分析 Date 使用不会修改时间的UTC时区。确认杰克逊的时区没有被修改。E、 g.如果 mapper ObjectMapper

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

    然后,您可以将时区设置为 setTimeZone

    如果无法修改Jackson的timezome设置,可以通过在中指定时区来覆盖字段的设置 @JsonFormat 注释:

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