1 package cn.common.config; 2 3 import com.fasterxml.jackson.databind.ObjectMapper; 4 import com.fasterxml.jackson.databind.module.SimpleModule; 5 import com.fasterxml.jackson.datatype.jsr310.PackageVersion; 6 import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer; 7 import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer; 8 import com.fasterxml.jackson.datatype.jsr310.deser.LocalTimeDeserializer; 9 import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer; 10 import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer; 11 import com.fasterxml.jackson.datatype.jsr310.ser.LocalTimeSerializer; 12 import org.springframework.boot.autoconfigure.AutoConfigureBefore; 13 import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; 14 import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer; 15 import org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration; 16 import org.springframework.context.annotation.Bean; 17 import org.springframework.context.annotation.Configuration; 18 19 import java.time.LocalDate; 20 import java.time.LocalDateTime; 21 import java.time.LocalTime; 22 import java.time.ZoneId; 23 import java.time.format.DateTimeFormatter; 24 import java.util.Locale; 25 import java.util.TimeZone; 26 27 /** 28 * JSON LocalDateTime 序列化、反序列化配置 29 */ 30 @Configuration 31 @ConditionalOnClass(ObjectMapper.class) 32 @AutoConfigureBefore(JacksonAutoConfiguration.class) 33 public class JsonLocalDateTimeConfig { 34 public static final String NORM_DATETIME_PATTERN = "yyyy-MM-dd HH:mm:ss"; 35 public static final String NORM_DATE_PATTERN = "yyyy-MM-dd"; 36 public static final String NORM_TIME_PATTERN = "HH:mm:ss"; 37 /** 38 * 时区配置 39 */ 40 @Bean 41 public Jackson2ObjectMapperBuilderCustomizer jacksonObjectMapperCustomization() { 42 return jacksonObjectMapperBuilder -> { 43 /** 44 * 配置Date的格式化 45 */ 46 jacksonObjectMapperBuilder.locale(Locale.CHINA); 47 jacksonObjectMapperBuilder.timeZone(TimeZone.getTimeZone(ZoneId.systemDefault())); 48 jacksonObjectMapperBuilder.simpleDateFormat(NORM_DATETIME_PATTERN); 49 /** 50 * 配置java8 时间配置 51 */ 52 jacksonObjectMapperBuilder.modules(new JavaLocalDateTimeModule()); 53 }; 54 } 55 56 ///配置自定义model 57 class JavaLocalDateTimeModule extends SimpleModule { 58 public JavaLocalDateTimeModule() { 59 super(PackageVersion.VERSION); 60 this.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(DateTimeFormatter.ofPattern( 61 NORM_DATETIME_PATTERN))); 62 this.addSerializer(LocalDate.class, new LocalDateSerializer(DateTimeFormatter.ofPattern(NORM_DATE_PATTERN))); 63 this.addSerializer(LocalTime.class, new LocalTimeSerializer(DateTimeFormatter.ofPattern(NORM_TIME_PATTERN))); 64 this.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern(NORM_DATETIME_PATTERN))); 65 this.addDeserializer(LocalDate.class, new LocalDateDeserializer(DateTimeFormatter.ofPattern(NORM_DATE_PATTERN))); 66 this.addDeserializer(LocalTime.class, new LocalTimeDeserializer(DateTimeFormatter.ofPattern(NORM_TIME_PATTERN))); 67 68 } 69 } 70 }