当指定语言和时区的时候再进行更改默认

This commit is contained in:
thinkgem
2025-09-24 13:32:22 +08:00
parent 862568c919
commit 84f42a27e5
2 changed files with 29 additions and 17 deletions

View File

@@ -28,10 +28,7 @@ import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
import java.io.IOException; import java.io.IOException;
import java.lang.reflect.AnnotatedElement; import java.lang.reflect.AnnotatedElement;
import java.util.Date; import java.util.*;
import java.util.List;
import java.util.Map;
import java.util.TimeZone;
/** /**
* 封装 Jackson实现 JSON String 与 Java Object 互转 * 封装 Jackson实现 JSON String 与 Java Object 互转
@@ -73,10 +70,17 @@ public class JsonMapper extends ObjectMapper {
* @author ThinkGem * @author ThinkGem
*/ */
public JsonMapper setLocaleTimeZoneDateFormat(){ public JsonMapper setLocaleTimeZoneDateFormat(){
this.setLocale(LocaleUtils.toLocale(PropertiesUtils.getInstance() PropertiesUtils props = PropertiesUtils.getInstance();
.getProperty("lang.defaultLocale", "zh_CN"))); // 设置默认语言环境
this.setTimeZone(TimeZone.getTimeZone(PropertiesUtils.getInstance() String defaultLocale = props.getProperty("lang.defaultLocale");
.getProperty("lang.defaultTimeZone", "GMT+08:00"))); 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() { this.setAnnotationIntrospector(new JacksonAnnotationIntrospector() {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
@Override @Override

View File

@@ -4,8 +4,8 @@
*/ */
package com.jeesite.common.mapper; package com.jeesite.common.mapper;
import com.fasterxml.jackson.databind.JavaType;
import com.jeesite.common.io.PropertiesUtils; import com.jeesite.common.io.PropertiesUtils;
import org.apache.commons.lang3.LocaleUtils;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@@ -20,7 +20,7 @@ import java.util.TimeZone;
* @version 2016-9-2 * @version 2016-9-2
*/ */
public class XmlMapper extends com.fasterxml.jackson.dataformat.xml.XmlMapper{ public class XmlMapper extends com.fasterxml.jackson.dataformat.xml.XmlMapper{
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
private static final Logger logger = LoggerFactory.getLogger(XmlMapper.class); 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() { 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 // Spring ObjectMapper 初始化配置,支持 @JsonView
new Jackson2ObjectMapperBuilder().configure(this); 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 { try {
return this.writeValueAsString(object); return this.writeValueAsString(object);
} catch (IOException e) { } catch (IOException e) {
logger.warn("write to xml string error:" + object, e); logger.warn("write to xml string error: {}", object, e);
return null; return null;
} }
} }
@@ -62,14 +70,14 @@ public class XmlMapper extends com.fasterxml.jackson.dataformat.xml.XmlMapper{
try { try {
return this.writerWithView(jsonView).writeValueAsString(object); return this.writerWithView(jsonView).writeValueAsString(object);
} catch (IOException e) { } catch (IOException e) {
logger.warn("write to xml string error:" + object, e); logger.warn("write to xml string error: {}", object, e);
return null; return null;
} }
} }
/** /**
* 反序列化POJO或简单Collection如List<String>. * 反序列化POJO或简单Collection如List<String>.
* @see #fromJson(String, JavaType) * @see #fromXml(String, Class)
*/ */
public <T> T fromXmlString(String xmlString, Class<T> clazz) { public <T> T fromXmlString(String xmlString, Class<T> clazz) {
if (StringUtils.isEmpty(xmlString) || "<CLOB>".equals(xmlString)) { if (StringUtils.isEmpty(xmlString) || "<CLOB>".equals(xmlString)) {
@@ -78,7 +86,7 @@ public class XmlMapper extends com.fasterxml.jackson.dataformat.xml.XmlMapper{
try { try {
return this.readValue(xmlString, clazz); return this.readValue(xmlString, clazz);
} catch (IOException e) { } catch (IOException e) {
logger.warn("parse xml string error:" + xmlString, e); logger.warn("parse xml string error: {}", xmlString, e);
return null; return null;
} }
} }