时间:2023-02-07 09:48:38 | 栏目:JAVA代码 | 点击:次
使用 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.jackson.databind.SerializationFeature.*; import static java.time.format.DateTimeFormatter.ofPattern; /<strong> * 配置类 * * @author D瓜哥 · <a href="https://www.diguage.com/" rel="external nofollow" rel="external nofollow" target="_blank" >https://www.diguage.com</a> */ @Configuration public class Config { /</strong> * 创建 ObjectMapper 对象,配置日期格式化 * * @author D瓜哥 · <a href="https://www.diguage.com/" 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; } }
不知道通过这种方式指定日期格式化样式后,在处理一些打格式化样式注解的字段时,会有什么样的表现?有机会测试一下。
方式一:配置文件yml中配置
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); } }; } }