SpringBoot中Jackson日期格式化技巧分享
目录
- Jackson 日期格式化技巧
- 后续问题
- 补充:Jackson 统一配置 日期转换格式
- 参考资料
Jackson 日期格式化技巧
使用 Spring Boot 时,需要使用 Jackson 处理一些 java Time API 类型的 jsON 序列化问题,在处理一些类的字段时,可以通过直接在属性上加注解的方式来指定其格式化样式。但是,昨天同事遇到一个格式化Map
数据的问题,这样就不能通过加注解来解决格式化样式的问题了。
在网上各种搜索,各种尝试后,终于解决了这个问题,记录一下,以备不时之需。
闲言少叙,直接上代码:
package com.diguage.demo.config; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.util.StdDateFormat; import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer; import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer; import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer; import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Primary; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.time.LocalDate; import java.time.LocalDateTime; import static com.fasterxml.jLTcoVyackson.databind.SerializationFeature.*; import static java.time.format.DateTimeFormatter.ofPattern; /<strong> * 配置类 * * @author D瓜哥 <a href="https://mip.qiaqa.com/uploads/article/2022/04/22/82" rel="external nofollow" rel="external nofollow" target="_blank" >https://www.diguage.com</a> */ @Configuration public class Config { /</strong> * 创建 ObjectMapper 对象,配置日期格式化 * * @author D瓜哥 <a href="https://mip.qiaqa.com/uploads/article/2022/04/22/82" rel="external nofollow" rel="external nofollow" target="_blank" >https://www.diguage.com</a> */ @Bean @Primary public ObjectMapper objectMapper() { ObjectMapper mapper = new ObjectMapper(); String dateTimepattern = "yyyy-MM-dd HH:mm:ss"; String datePattern = "yyyy-MM-dd"; DateFormat dateFormat = new SimpleDateFormat(dateTimepattern); mapper.setDateFormat(dateFormat); mapper.configure(WRITE_DATES_AS_TIMESTAMPS, false); mapper.setDateFormat(new StdDateFormat().withColonInTimeZone(true)); JavaTimeModule javaTimeModule = new JavaTimeModule(); javaTimeModule.addDeserializer(LocalDate.class, new LocalDateDeserializer(ofPattern(datePattern))); javaTimeModule.addSerializer(LocalDate.class, new LocalDateSerializer(ofPattern(datePattern))); javaTimeModule.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(ofPattern(dateTimepattern))); javaTimeModule.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(ofPattern(dateTimepattern))); mapper.registerModule(javaTimeModule); return mapper; } }
后续问题
不知道通过这种方式指定日期格式化样式后,在处理一些打格式化样式注解的字段时,会有什么样的表现?有机会测试一下。
补充:Jackson 统一配置 日期转换格式
spring: jackson: default-property-inclusion: ALWAYS time-zone: GMT+8 date-format: yyyy-MM-dd HH:mm:ss
这样序列化后,Date类型会被格式化成配置中的格式。
创建JacksonConfig.java
@Configuration public class JacksonConfig { @Bean @Order(Ordered.HIGHEST_PRECEDENCE) public Jackson2ObjectMapperBuilderCustomizer customJackson() { return new Jackson2ObjectMapperBuilderCustomizer() { @Override public void customize(Jackson2ObjectMapperBuilder builder) { builder.serializerByType(LocalDateTime.class, new LocalDateTimeSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))); builder.serializerByType(LocalDate.class, new LocalDateSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd"))); builder.serializerByType(LocalTime.class, new LocalTimeSerializer(DateTimeFormatter.ofPattern("HH:mm:ss"))); builder.deserializerByType(LocalDateTime.class, new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))); builder.deserializerByType(LocalDate.class, new LocalDateDeserializer(DateTimeFormatter.ofPattern("yyyy-MM-dd"))); builder.deserializerByType(LocalTime.class, new LocalTimeDeserializer(DateTimeFormatter.ofPattern("HH:mm:ss"))); builder.serializationInclusion(JsonInclude.Include.NON_NULL); builder.failOnUnknownProperties(false); builder.featuresToDisable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS); } }; } }
参考资料
Jackson Date
json - Java 8 LocalDate Jackson format
到此这篇关于SpringBoot中Jackson日期格式化技巧的文章就介绍到这了,更多相关SpringBootJackson日期格式化内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!
推荐阅读
-
每个Java程序员必备的8个开发工具
本文由码农网 –王国峰原创翻译,转载请看清文末的转载要求,欢迎参与我们的付费投稿计划!现在有很多库、实用工具和程序任J...
-
Java实战之医院管理系统的实现
目录项目介绍环境需要技术栈使用说明效果图展示核心代码用户管理控制层医生管理控制层病房管理控制层项目介绍医院管理系统,分为管理员、医...
-
elasticsearch索引index之Translog数据功能分析
目录translog的结构及写入方式translogFile的继承关系TranslogFile快照的方法总结translog的结构...
-
java实现简单发送邮件功能
-
Java实现图片比率缩放
-
Java中的JetCache 实战
-
elasticsearch索引index之engine读写控制结构实现
目录engine的实现结构Engine类的方法:如index方法的实现:总结engine的实现结构elasticsearch对于...
-
elasticsearch索引index之Mapping实现关系结构示例
目录Mapping的实现关系结构Mapper的三类parse方法部分Field总结Mapping的实现关系结构Lucene索引的...
-
LeetCode 动态规划之矩阵区域和详情
目录题目题解解题分析解题代码题目矩阵区域和给你一个mxn的矩阵mat和一个整数k,请你返回一个矩阵answer,其中每个a...
-
elasticsearch索引的创建过程index create逻辑分析
目录索引的创建过程materOperation方法实现clusterservice处理建立索引修改配置总结索引的创建过程从本篇...