From 5a96c23882974b5920ae494bf817e542a36bcaed Mon Sep 17 00:00:00 2001
From: yexuejc <1107047387@qq.com>
Date: Sat, 24 Apr 2021 00:52:35 +0800
Subject: [PATCH] =?UTF-8?q?1.4.4=20JsonUtil=E7=B1=BB=E4=BF=AE=E5=A4=8D?=
=?UTF-8?q?=E6=A0=BC=E5=BC=8F=E5=8C=96=E5=87=BA=E7=8E=B0=E7=9A=84=E6=97=B6?=
=?UTF-8?q?=E5=8C=BA=E5=B7=AE?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
UPDATE.md | 6 +++++
pom.xml | 2 +-
.../java/com/yexuejc/base/util/JsonUtil.java | 23 ++++++++++++++++++-
3 files changed, 29 insertions(+), 2 deletions(-)
diff --git a/UPDATE.md b/UPDATE.md
index aef15f4..37be89b 100644
--- a/UPDATE.md
+++ b/UPDATE.md
@@ -1,6 +1,12 @@
yexuejc-base 更新记录
------------------
+#### version :1.4.4
+**time:2021-4-24 00:41:31**
+**branch:** master
+**update:**
+>1. JsonUtil类修复格式化出现的时区差:现使用TimeZone.getDefault()时区
+#
#### version :1.4.3
**time:2021-2-6 11:42:49**
**branch:** master
diff --git a/pom.xml b/pom.xml
index 1163c67..fb9b02a 100644
--- a/pom.xml
+++ b/pom.xml
@@ -6,7 +6,7 @@
top.yexuejc
yexuejc-base
- 1.4.3
+ 1.4.4
${project.artifactId}
https://github.com/yexuejc/yexuejc-base
diff --git a/src/main/java/com/yexuejc/base/util/JsonUtil.java b/src/main/java/com/yexuejc/base/util/JsonUtil.java
index b7a4c6d..2225370 100644
--- a/src/main/java/com/yexuejc/base/util/JsonUtil.java
+++ b/src/main/java/com/yexuejc/base/util/JsonUtil.java
@@ -10,6 +10,7 @@ import com.fasterxml.jackson.databind.type.MapType;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;
+import java.util.TimeZone;
import java.util.logging.Logger;
/**
@@ -46,6 +47,8 @@ public class JsonUtil {
objectMapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
+ //设置一下时区,可以和程序同步避免时区问题
+ objectMapper.setTimeZone(TimeZone.getDefault());
objectMapper.setDateFormat(DateUtil.DATE_TIME_FORMAT);
}
@@ -55,6 +58,15 @@ public class JsonUtil {
objectMapper.setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE);
}
+ /**
+ * 这个设置不能改变JsonUtil自带的objectMapper设置,只能修改传入objMapper的设置
+ * @param objMapper
+ */
+ public static void initSnakeCase(ObjectMapper objMapper) {
+ //驼峰下划线互转
+ objMapper.setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE);
+ }
+
/**
* 每调用一次生成一个全新的ObjectMapper供特殊场景使用,与通用ObjectMapper没有关系
*
@@ -66,6 +78,15 @@ public class JsonUtil {
return objectMapper;
}
+ /**
+ * 返回 ObjectMapper对象,供外部设置特定参数
+ *
+ * @return
+ */
+ public static ObjectMapper getObjectMapper() {
+ return objectMapper;
+ }
+
/**
* 将json转换为某个类
*
@@ -106,7 +127,7 @@ public class JsonUtil {
log.warning("json to Object JsonMappingException.\n" + e.getMessage());
} catch (IOException e) {
log.warning("json to Object IOException.\n" + e.getMessage());
- }catch (Exception e){
+ } catch (Exception e) {
e.printStackTrace();
}