From 84f42a27e544fd04913eb5ea4a912bb680b8a3be Mon Sep 17 00:00:00 2001 From: thinkgem Date: Wed, 24 Sep 2025 13:32:22 +0800 Subject: [PATCH] =?UTF-8?q?=E5=BD=93=E6=8C=87=E5=AE=9A=E8=AF=AD=E8=A8=80?= =?UTF-8?q?=E5=92=8C=E6=97=B6=E5=8C=BA=E7=9A=84=E6=97=B6=E5=80=99=E5=86=8D?= =?UTF-8?q?=E8=BF=9B=E8=A1=8C=E6=9B=B4=E6=94=B9=E9=BB=98=E8=AE=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/jeesite/common/mapper/JsonMapper.java | 20 ++++++++------ .../com/jeesite/common/mapper/XmlMapper.java | 26 ++++++++++++------- 2 files changed, 29 insertions(+), 17 deletions(-) diff --git a/common/src/main/java/com/jeesite/common/mapper/JsonMapper.java b/common/src/main/java/com/jeesite/common/mapper/JsonMapper.java index dc1bcb4a..cb223085 100644 --- a/common/src/main/java/com/jeesite/common/mapper/JsonMapper.java +++ b/common/src/main/java/com/jeesite/common/mapper/JsonMapper.java @@ -28,10 +28,7 @@ import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder; import java.io.IOException; import java.lang.reflect.AnnotatedElement; -import java.util.Date; -import java.util.List; -import java.util.Map; -import java.util.TimeZone; +import java.util.*; /** * 封装 Jackson,实现 JSON String 与 Java Object 互转 @@ -73,10 +70,17 @@ public class JsonMapper extends ObjectMapper { * @author ThinkGem */ public JsonMapper setLocaleTimeZoneDateFormat(){ - this.setLocale(LocaleUtils.toLocale(PropertiesUtils.getInstance() - .getProperty("lang.defaultLocale", "zh_CN"))); - this.setTimeZone(TimeZone.getTimeZone(PropertiesUtils.getInstance() - .getProperty("lang.defaultTimeZone", "GMT+08:00"))); + PropertiesUtils props = PropertiesUtils.getInstance(); + // 设置默认语言环境 + String defaultLocale = props.getProperty("lang.defaultLocale"); + if (StringUtils.isNotBlank(defaultLocale)) { + this.setLocale(LocaleUtils.toLocale(defaultLocale)); + } + // 设置默认时区 + String defaultTimeZone = props.getProperty("lang.defaultTimeZone"); + if (StringUtils.isNotBlank(defaultTimeZone)) { + this.setTimeZone(TimeZone.getTimeZone(defaultTimeZone)); + } this.setAnnotationIntrospector(new JacksonAnnotationIntrospector() { private static final long serialVersionUID = 1L; @Override diff --git a/common/src/main/java/com/jeesite/common/mapper/XmlMapper.java b/common/src/main/java/com/jeesite/common/mapper/XmlMapper.java index c6ab69b2..f25e19aa 100644 --- a/common/src/main/java/com/jeesite/common/mapper/XmlMapper.java +++ b/common/src/main/java/com/jeesite/common/mapper/XmlMapper.java @@ -4,8 +4,8 @@ */ package com.jeesite.common.mapper; -import com.fasterxml.jackson.databind.JavaType; import com.jeesite.common.io.PropertiesUtils; +import org.apache.commons.lang3.LocaleUtils; import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -20,7 +20,7 @@ import java.util.TimeZone; * @version 2016-9-2 */ public class XmlMapper extends com.fasterxml.jackson.dataformat.xml.XmlMapper{ - + private static final long serialVersionUID = 1L; private static final Logger logger = LoggerFactory.getLogger(XmlMapper.class); @@ -36,11 +36,19 @@ public class XmlMapper extends com.fasterxml.jackson.dataformat.xml.XmlMapper{ * 构造方法 */ public XmlMapper() { + PropertiesUtils props = PropertiesUtils.getInstance(); + // 设置默认语言环境 + String defaultLocale = props.getProperty("lang.defaultLocale"); + if (StringUtils.isNotBlank(defaultLocale)) { + this.setLocale(LocaleUtils.toLocale(defaultLocale)); + } + // 设置默认时区 + String defaultTimeZone = props.getProperty("lang.defaultTimeZone"); + if (StringUtils.isNotBlank(defaultTimeZone)) { + this.setTimeZone(TimeZone.getTimeZone(defaultTimeZone)); + } // Spring ObjectMapper 初始化配置,支持 @JsonView new Jackson2ObjectMapperBuilder().configure(this); - // 设置默认时区 - this.setTimeZone(TimeZone.getTimeZone(PropertiesUtils.getInstance() - .getProperty("lang.defaultTimeZone", "GMT+08:00"))); } /** @@ -50,7 +58,7 @@ public class XmlMapper extends com.fasterxml.jackson.dataformat.xml.XmlMapper{ try { return this.writeValueAsString(object); } catch (IOException e) { - logger.warn("write to xml string error:" + object, e); + logger.warn("write to xml string error: {}", object, e); return null; } } @@ -62,14 +70,14 @@ public class XmlMapper extends com.fasterxml.jackson.dataformat.xml.XmlMapper{ try { return this.writerWithView(jsonView).writeValueAsString(object); } catch (IOException e) { - logger.warn("write to xml string error:" + object, e); + logger.warn("write to xml string error: {}", object, e); return null; } } /** * 反序列化POJO或简单Collection如List. - * @see #fromJson(String, JavaType) + * @see #fromXml(String, Class) */ public T fromXmlString(String xmlString, Class clazz) { if (StringUtils.isEmpty(xmlString) || "".equals(xmlString)) { @@ -78,7 +86,7 @@ public class XmlMapper extends com.fasterxml.jackson.dataformat.xml.XmlMapper{ try { return this.readValue(xmlString, clazz); } catch (IOException e) { - logger.warn("parse xml string error:" + xmlString, e); + logger.warn("parse xml string error: {}", xmlString, e); return null; } }