news 2026/4/23 20:08:33

Spring Boot全局日期格式配置方法

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
Spring Boot全局日期格式配置方法
spring boot中,不想每个实体类中都这样设置,有什么方法: @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss") private LocalDateTime createTime;

在Spring Boot中有几种方法可以避免在每个实体类中重复设置@JsonFormat注解:

方法一:全局配置(推荐)

application.ymlapplication.properties中配置全局的日期格式:

YAML配置:

yaml

spring: jackson: date-format: yyyy-MM-dd HH:mm:ss time-zone: GMT+8 serialization: write-dates-as-timestamps: false

Properties配置:

properties

spring.jackson.date-format=yyyy-MM-dd HH:mm:ss spring.jackson.time-zone=GMT+8 spring.jackson.serialization.write-dates-as-timestamps=false

方法二:自定义Jackson配置类

java

@Configuration public class JacksonConfig { @Bean @Primary public Jackson2ObjectMapperBuilderCustomizer jacksonCustomizer() { return builder -> { // 设置全局日期格式 builder.simpleDateFormat("yyyy-MM-dd HH:mm:ss"); builder.timeZone(TimeZone.getTimeZone("GMT+8")); // 针对Java 8时间API的配置 builder.serializers(new LocalDateTimeSerializer( DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))); builder.deserializers(new LocalDateTimeDeserializer( DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))); // 如果需要,也可以配置LocalDate等其他类型 builder.serializers(new LocalDateSerializer( DateTimeFormatter.ofPattern("yyyy-MM-dd"))); builder.deserializers(new LocalDateDeserializer( DateTimeFormatter.ofPattern("yyyy-MM-dd"))); }; } }

方法三:更详细的配置类

java

@Configuration public class JacksonConfig { @Bean @Primary public ObjectMapper objectMapper() { ObjectMapper objectMapper = new ObjectMapper(); // 设置Java 8时间模块 JavaTimeModule javaTimeModule = new JavaTimeModule(); // LocalDateTime序列化和反序列化 javaTimeModule.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))); javaTimeModule.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))); // LocalDate序列化和反序列化 javaTimeModule.addSerializer(LocalDate.class, new LocalDateSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd"))); javaTimeModule.addDeserializer(LocalDate.class, new LocalDateDeserializer(DateTimeFormatter.ofPattern("yyyy-MM-dd"))); objectMapper.registerModule(javaTimeModule); objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS); objectMapper.setTimeZone(TimeZone.getTimeZone("GMT+8")); return objectMapper; } }

方法四:使用Mixin(为特定类型统一配置)

java

@JsonDeserialize(using = LocalDateTimeDeserializer.class) @JsonSerialize(using = LocalDateTimeSerializer.class) @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss") abstract class LocalDateTimeMixin { } @Configuration public class JacksonConfig { @Bean public Jackson2ObjectMapperBuilderCustomizer addCustomSerialization() { return builder -> { builder.mixIn(LocalDateTime.class, LocalDateTimeMixin.class); }; } }

方法五:自定义注解(如果需要不同格式)

java

@Retention(RetentionPolicy.RUNTIME) @JacksonAnnotationsInside @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss") public @interface DefaultDateTimeFormat { } // 使用方式 @DefaultDateTimeFormat private LocalDateTime createTime;

推荐方案

建议使用方法一(全局配置),最简单直接,能满足大部分场景。

如果项目中有多种不同的日期格式需求,可以:

  1. 使用方法一设置默认格式

  2. 对于特殊格式的字段,使用@JsonFormat覆盖全局配置

java

// 全局使用 yyyy-MM-dd HH:mm:ss // 特殊字段使用其他格式 @JsonFormat(pattern = "yyyy/MM/dd") private LocalDate specialDate;

这样既能减少重复代码,又能保持灵活性。

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/4/19 21:57:07

pyimgui:Python立即模式GUI开发的终极指南

pyimgui:Python立即模式GUI开发的终极指南 【免费下载链接】pyimgui Cython-based Python bindings for dear imgui 项目地址: https://gitcode.com/gh_mirrors/py/pyimgui pyimgui是基于Cython开发的Python绑定库,为Dear ImGui提供完整的Python支…

作者头像 李华
网站建设 2026/4/23 3:10:58

OpCore Simplify终极指南:从零构建完美Hackintosh的完整攻略

OpCore Simplify终极指南:从零构建完美Hackintosh的完整攻略 【免费下载链接】OpCore-Simplify A tool designed to simplify the creation of OpenCore EFI 项目地址: https://gitcode.com/GitHub_Trending/op/OpCore-Simplify 探索OpCore Simplify自动化工…

作者头像 李华
网站建设 2026/4/19 14:07:03

如何快速制作Windows启动盘:WindiskWriter完整使用指南

如何快速制作Windows启动盘:WindiskWriter完整使用指南 【免费下载链接】windiskwriter 🖥 A macOS app that creates bootable USB drives for Windows. 🛠 Patches Windows 11 to bypass TPM and Secure Boot requirements. 项目地址: ht…

作者头像 李华
网站建设 2026/4/19 18:03:22

FaceFusion与GraphCMS结合:GraphQL接口的灵活调用

FaceFusion与GraphCMS结合:GraphQL接口的灵活调用 在内容创作日益智能化的今天,一个编辑不再需要手动导出图像、运行Python脚本、再将结果上传回后台——理想的工作流应当是:她在CMS中选中两张照片,点击“生成”,几秒后…

作者头像 李华
网站建设 2026/4/20 18:37:38

快速掌握ghettoVCB:VMware备份终极配置指南

快速掌握ghettoVCB:VMware备份终极配置指南 【免费下载链接】ghettoVCB ghettoVCB 项目地址: https://gitcode.com/gh_mirrors/gh/ghettoVCB ghettoVCB是一款专为VMware ESXi环境设计的开源备份解决方案,提供轻量级虚拟机数据保护功能。这个强大的…

作者头像 李华
网站建设 2026/4/22 10:38:47

SonarQube的轻量替代:sourcefare,开源免费代码扫描工具

研发过程中,为了持续检测和改进代码质量,会使用一些代码扫描工具。例如SonarQube,SonarQube是一款代码质量管理工具,能通过静态分析检测代码缺陷、漏洞、重复和异味。但是部分语言收费,细粒度分支分析、安全扫描也需企…

作者头像 李华