You need to enable JavaScript to run this app. Spring Boot Reactive中如何配置LocalTime/LocalDate按ISO格式序列化为字符串
优惠活动
大模型
产品
解决方案
定价
更多

Spring Boot Reactive中如何配置LocalTime/LocalDate按ISO格式序列化为字符串

问题根因

原有配置失效的两个核心原因:

  • 仅注册JavaTimeModule,未关闭Jackson默认开启的WRITE_DATES_AS_TIMESTAMPS特性。该特性开启时,即使注册了Java 8时间序列化模块,Jackson也会将时间类拆解为独立字段输出,而非ISO格式字符串。
  • Spring Boot WebFlux(Reactive)的消息转换器加载逻辑与传统Spring MVC存在差异,仅通过@Primary修饰的自定义ObjectMapper Bean,存在无法被自动配置的Jackson编解码器正确识别的概率,与Swagger等组件共存时,甚至会出现接口实际返回与Swagger文档展示结构不一致的问题。
有效配置方案

方案1:最简零代码配置(推荐)

直接在项目配置文件中添加对应配置,Spring Boot会自动适配MVC和Reactive场景下的Jackson序列化逻辑,不需要手动编写Bean类:
如果使用application.properties,添加如下配置:

# 关闭时间类按拆解对象/时间戳数组序列化
spring.jackson.serialization.write-dates-as-timestamps=false

如果使用application.yml,对应配置为:

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

注意:不需要手动注册JavaTimeModule,Spring Boot的Jackson自动配置在检测到classpath下存在jackson-datatype-jsr310依赖时,会自动完成模块注册,手动重复注册反而可能引发版本冲突。

方案2:自定义代码配置(适合需要扩展其他Jackson规则的场景)

如果需要同时自定义其他序列化逻辑,可修改原有配置类,补充关闭时间戳序列化的核心逻辑:

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;

@Configuration
public class MyConfiguration {

    @Bean
    @Primary
    public ObjectMapper customJson() {
        return new Jackson2ObjectMapperBuilder()
                .modulesToInstall(new JavaTimeModule())
                // 核心:关闭时间类拆解序列化逻辑
                .featuresToDisable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
                .build();
    }
}

如果配置后仍未生效,可补充WebFlux专属的编解码器配置,确保自定义ObjectMapper被正式加载:

import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.http.codec.ServerCodecConfigurer;
import org.springframework.http.codec.json.Jackson2JsonDecoder;
import org.springframework.http.codec.json.Jackson2JsonEncoder;
import org.springframework.web.reactive.config.WebFluxConfigurer;

@Configuration
public class WebFluxConfig implements WebFluxConfigurer {

    private final ObjectMapper customJson;

    public WebFluxConfig(ObjectMapper customJson) {
        this.customJson = customJson;
    }

    @Override
    public void configureHttpMessageCodecs(ServerCodecConfigurer configurer) {
        configurer.defaultCodecs().jackson2JsonEncoder(new Jackson2JsonEncoder(customJson, MediaType.APPLICATION_JSON));
        configurer.defaultCodecs().jackson2JsonDecoder(new Jackson2JsonDecoder(customJson, MediaType.APPLICATION_JSON));
    }
}
验证标准

配置完成后重启项目,LocalTime/LocalDate/LocalDateTime字段会自动输出为标准ISO格式字符串,例如:

  • LocalTime: "endTime": "00:00:00"
  • LocalDate: "date": "2024-05-20"
  • LocalDateTime: "createTime": "2024-05-20T12:30:00"

内容的提问来源于stack exchange,提问作者Askolein

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.11.14 17:24:03