code optimization

This commit is contained in:
thinkgem
2025-10-09 23:35:12 +08:00
parent 9b85753948
commit 92d4161294
3 changed files with 12 additions and 18 deletions

View File

@@ -74,15 +74,13 @@ public class JsonMapper extends ObjectMapper {
public JsonMapper setLocaleTimeZoneDateFormat(){
PropertiesUtils props = PropertiesUtils.getInstance();
// 设置默认语言环境
String defaultLocale = props.getProperty("lang.defaultLocale");
if (StringUtils.isNotBlank(defaultLocale)) {
props.getPropertyIfNotBlank("lang.defaultLocale", (defaultLocale) -> {
this.setLocale(LocaleUtils.toLocale(defaultLocale));
}
});
// 设置默认时区
String defaultTimeZone = props.getProperty("lang.defaultTimeZone");
if (StringUtils.isNotBlank(defaultTimeZone)) {
props.getPropertyIfNotBlank("lang.defaultTimeZone", (defaultTimeZone) -> {
this.setTimeZone(TimeZone.getTimeZone(defaultTimeZone));
}
});
this.setAnnotationIntrospector(new JacksonAnnotationIntrospector() {
@Serial
private static final long serialVersionUID = 1L;

View File

@@ -40,15 +40,13 @@ 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)) {
props.getPropertyIfNotBlank("lang.defaultLocale", (defaultLocale) -> {
this.setLocale(LocaleUtils.toLocale(defaultLocale));
}
});
// 设置默认时区
String defaultTimeZone = props.getProperty("lang.defaultTimeZone");
if (StringUtils.isNotBlank(defaultTimeZone)) {
props.getPropertyIfNotBlank("lang.defaultTimeZone", (defaultTimeZone) -> {
this.setTimeZone(TimeZone.getTimeZone(defaultTimeZone));
}
});
// Spring ObjectMapper 初始化配置,支持 @JsonView
new Jackson2ObjectMapperBuilder().configure(this);
}

View File

@@ -15,12 +15,10 @@ import jakarta.servlet.http.HttpServletResponse;
*/
public class ResultUtils {
private static final boolean isDefaultResult = PropertiesUtils.getInstance()
.getPropertyToBoolean("web.isDefaultResult", "false");
private static final String resultParamName = PropertiesUtils.getInstance()
.getProperty("web.resultParamName", "__data");
private static final String headerParamName = PropertiesUtils.getInstance()
.getProperty("web.headerParamName", "x-data");
private static final PropertiesUtils props = PropertiesUtils.getInstance();
private static final boolean isDefaultResult = props.getPropertyToBoolean("web.isDefaultResult", "false");
private static final String resultParamName = props.getProperty("web.resultParamName", "__data");
private static final String headerParamName = props.getProperty("web.headerParamName", "x-data");
/**
* 设置 web.isResult 参数可强制全局使用统一结果输出,否则,传递 __data=true 参数,或 x-data 请求头为 true 时启用