Procházet zdrojové kódy

add 新增 控制时间显示页面 带T情况

bzd_lxf před 1 měsícem
rodič
revize
a81b2b635e

+ 40 - 0
health-framework/src/main/java/com/bzd/framework/config/WebMvcConfig.java

@@ -0,0 +1,40 @@
+package com.bzd.framework.config;
+
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
+import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
+
+import java.time.LocalDateTime;
+import java.time.format.DateTimeFormatter;
+
+/**
+ * 添加日期时间处理程序返回时间带2023-10-10T14:30:00 问题
+ *
+ * @author LiXiagnFei
+ */
+@Configuration
+public class WebMvcConfig {
+
+    @Bean
+    public MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter() {
+        MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
+        ObjectMapper mapper = new ObjectMapper();
+
+        // 添加 Java 8 时间模块
+        JavaTimeModule module = new JavaTimeModule();
+        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
+        module.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(formatter));
+
+        // 注册模块
+        mapper.registerModule(module);
+
+        // 设置全局日期时间格式
+        mapper.setDateFormat(new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
+
+        converter.setObjectMapper(mapper);
+        return converter;
+    }
+}