|
@@ -0,0 +1,51 @@
|
|
|
|
|
+package com.ydtech.config;
|
|
|
|
|
+
|
|
|
|
|
+import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
|
|
|
|
|
+import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
|
|
|
|
|
+import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
|
|
|
|
|
+import org.springframework.context.annotation.Bean;
|
|
|
|
|
+import org.springframework.context.annotation.Configuration;
|
|
|
|
|
+
|
|
|
|
|
+import java.time.LocalDateTime;
|
|
|
|
|
+import java.time.format.DateTimeFormatter;
|
|
|
|
|
+
|
|
|
|
|
+/**
|
|
|
|
|
+ * @author wenks
|
|
|
|
|
+ * @version 0.0.2
|
|
|
|
|
+ * @description LocalDateTime 全局配置
|
|
|
|
|
+ * @date 2023/5/17 17:50
|
|
|
|
|
+ */
|
|
|
|
|
+@Configuration
|
|
|
|
|
+public class LocalDateTimeConfig {
|
|
|
|
|
+
|
|
|
|
|
+ private static final DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 序列化内容
|
|
|
|
|
+ * LocalDateTime -> String
|
|
|
|
|
+ * 服务端返回给客户端内容
|
|
|
|
|
+ */
|
|
|
|
|
+ @Bean
|
|
|
|
|
+ public LocalDateTimeSerializer localDateTimeSerializer() {
|
|
|
|
|
+ return new LocalDateTimeSerializer(dateTimeFormatter);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /*
|
|
|
|
|
+ * 反序列化内容
|
|
|
|
|
+ * String -> LocalDateTime
|
|
|
|
|
+ * 客户端传入服务端数据
|
|
|
|
|
+ * */
|
|
|
|
|
+ @Bean
|
|
|
|
|
+ public LocalDateTimeDeserializer localDateTimeDeserializer() {
|
|
|
|
|
+ return new LocalDateTimeDeserializer(dateTimeFormatter);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Bean
|
|
|
|
|
+ public Jackson2ObjectMapperBuilderCustomizer jackson2ObjectMapperBuilderCustomizer() {
|
|
|
|
|
+ return builder -> {
|
|
|
|
|
+ builder.serializerByType(LocalDateTime.class, localDateTimeSerializer());
|
|
|
|
|
+ builder.deserializerByType(LocalDateTime.class, localDateTimeDeserializer());
|
|
|
|
|
+ };
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+}
|