mirror of
https://gitee.com/dromara/RuoYi-Vue-Plus.git
synced 2026-09-18 17:38:48 +08:00
update 优化特定场景下JsonUtils会导致非必要的对象序列化
This commit is contained in:
parent
4b04a4bf09
commit
5b89ff402c
@ -80,7 +80,7 @@ public class DefaultExcelListener<T> extends AnalysisEventListener<T> implements
|
|||||||
@Override
|
@Override
|
||||||
public void invokeHeadMap(Map<Integer, String> headMap, AnalysisContext context) {
|
public void invokeHeadMap(Map<Integer, String> headMap, AnalysisContext context) {
|
||||||
this.headMap = headMap;
|
this.headMap = headMap;
|
||||||
log.debug("解析到一条表头数据: {}", JsonUtils.toJsonString(headMap));
|
log.debug("解析到一条表头数据: {}", JsonUtils.toSafeJsonString(headMap));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@ -111,7 +111,7 @@ public class RepeatSubmitAspect {
|
|||||||
}
|
}
|
||||||
for (Object o : paramsArray) {
|
for (Object o : paramsArray) {
|
||||||
if (ObjectUtil.isNotNull(o) && !isFilterObject(o)) {
|
if (ObjectUtil.isNotNull(o) && !isFilterObject(o)) {
|
||||||
params.add(JsonUtils.toJsonString(o));
|
params.add(JsonUtils.toSafeJsonString(o));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return params.toString();
|
return params.toString();
|
||||||
|
|||||||
@ -1,5 +1,9 @@
|
|||||||
package org.dromara.common.json.config;
|
package org.dromara.common.json.config;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.annotation.JsonInclude;
|
||||||
|
import com.fasterxml.jackson.databind.DeserializationFeature;
|
||||||
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
import com.fasterxml.jackson.databind.SerializationFeature;
|
||||||
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
|
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
|
||||||
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
|
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
|
||||||
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
|
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
|
||||||
@ -47,4 +51,29 @@ public class JacksonConfig {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Bean("plusObjectMapper")
|
||||||
|
public ObjectMapper plusObjectMapper() {
|
||||||
|
ObjectMapper mapper = new ObjectMapper();
|
||||||
|
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
|
||||||
|
mapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
|
||||||
|
mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
|
||||||
|
|
||||||
|
JavaTimeModule javaTimeModule = new JavaTimeModule();
|
||||||
|
javaTimeModule.addSerializer(Long.class, BigNumberSerializer.INSTANCE);
|
||||||
|
javaTimeModule.addSerializer(Long.TYPE, BigNumberSerializer.INSTANCE);
|
||||||
|
javaTimeModule.addSerializer(BigInteger.class, BigNumberSerializer.INSTANCE);
|
||||||
|
javaTimeModule.addSerializer(BigDecimal.class, ToStringSerializer.instance);
|
||||||
|
|
||||||
|
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
|
||||||
|
javaTimeModule.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(formatter));
|
||||||
|
javaTimeModule.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(formatter));
|
||||||
|
javaTimeModule.addDeserializer(Date.class, new CustomDateDeserializer());
|
||||||
|
|
||||||
|
mapper.registerModule(javaTimeModule);
|
||||||
|
mapper.setTimeZone(TimeZone.getDefault());
|
||||||
|
|
||||||
|
log.info("初始化 plusObjectMapper 配置");
|
||||||
|
return mapper;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -25,17 +25,20 @@ import java.util.List;
|
|||||||
public class JsonUtils {
|
public class JsonUtils {
|
||||||
|
|
||||||
private static final ObjectMapper OBJECT_MAPPER = SpringUtils.getBean(ObjectMapper.class);
|
private static final ObjectMapper OBJECT_MAPPER = SpringUtils.getBean(ObjectMapper.class);
|
||||||
|
private static final ObjectMapper PLUS_MAPPER = SpringUtils.getBean("plusObjectMapper", ObjectMapper.class);
|
||||||
|
|
||||||
public static ObjectMapper getObjectMapper() {
|
public static ObjectMapper getObjectMapper() {
|
||||||
return OBJECT_MAPPER;
|
return OBJECT_MAPPER;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 将对象转换为JSON格式的字符串
|
* 将对象转换为 JSON 字符串(使用全局增强版 ObjectMapper,包含字段翻译、脱敏等功能)
|
||||||
|
* <p>
|
||||||
|
* 注意:此方法会触发全局 BeanSerializerModifier,例如多语言翻译或字段脱敏
|
||||||
|
* 建议仅用于实际响应数据或业务处理。
|
||||||
*
|
*
|
||||||
* @param object 要转换的对象
|
* @param object 要转换的对象
|
||||||
* @return JSON格式的字符串,如果对象为null,则返回null
|
* @return JSON 字符串
|
||||||
* @throws RuntimeException 如果转换过程中发生JSON处理异常,则抛出运行时异常
|
|
||||||
*/
|
*/
|
||||||
public static String toJsonString(Object object) {
|
public static String toJsonString(Object object) {
|
||||||
if (ObjectUtil.isNull(object)) {
|
if (ObjectUtil.isNull(object)) {
|
||||||
@ -48,6 +51,25 @@ public class JsonUtils {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 将对象转换为 JSON 字符串(使用“纯净” ObjectMapper,不触发翻译、脱敏等增强逻辑)
|
||||||
|
* <p>
|
||||||
|
* 用于日志打印、幂等校验、缓存 Key 等场景,避免性能开销或逻辑副作用
|
||||||
|
*
|
||||||
|
* @param object 要转换的对象
|
||||||
|
* @return JSON 字符串
|
||||||
|
*/
|
||||||
|
public static String toSafeJsonString(Object object) {
|
||||||
|
if (ObjectUtil.isNull(object)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
return PLUS_MAPPER.writeValueAsString(object);
|
||||||
|
} catch (JsonProcessingException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 将JSON格式的字符串转换为指定类型的对象
|
* 将JSON格式的字符串转换为指定类型的对象
|
||||||
*
|
*
|
||||||
|
|||||||
@ -145,7 +145,7 @@ public class LogAspect {
|
|||||||
}
|
}
|
||||||
// 是否需要保存response,参数和值
|
// 是否需要保存response,参数和值
|
||||||
if (log.isSaveResponseData() && ObjectUtil.isNotNull(jsonResult)) {
|
if (log.isSaveResponseData() && ObjectUtil.isNotNull(jsonResult)) {
|
||||||
operLog.setJsonResult(StringUtils.substring(JsonUtils.toJsonString(jsonResult), 0, 3800));
|
operLog.setJsonResult(StringUtils.substring(JsonUtils.toSafeJsonString(jsonResult), 0, 3800));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -164,7 +164,7 @@ public class LogAspect {
|
|||||||
} else {
|
} else {
|
||||||
MapUtil.removeAny(paramsMap, EXCLUDE_PROPERTIES);
|
MapUtil.removeAny(paramsMap, EXCLUDE_PROPERTIES);
|
||||||
MapUtil.removeAny(paramsMap, excludeParamNames);
|
MapUtil.removeAny(paramsMap, excludeParamNames);
|
||||||
operLog.setOperParam(StringUtils.substring(JsonUtils.toJsonString(paramsMap), 0, 3800));
|
operLog.setOperParam(StringUtils.substring(JsonUtils.toSafeJsonString(paramsMap), 0, 3800));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -178,12 +178,12 @@ public class LogAspect {
|
|||||||
}
|
}
|
||||||
for (Object o : paramsArray) {
|
for (Object o : paramsArray) {
|
||||||
if (ObjectUtil.isNotNull(o) && !isFilterObject(o)) {
|
if (ObjectUtil.isNotNull(o) && !isFilterObject(o)) {
|
||||||
String str = JsonUtils.toJsonString(o);
|
String str = JsonUtils.toSafeJsonString(o);
|
||||||
Dict dict = JsonUtils.parseMap(str);
|
Dict dict = JsonUtils.parseMap(str);
|
||||||
if (MapUtil.isNotEmpty(dict)) {
|
if (MapUtil.isNotEmpty(dict)) {
|
||||||
MapUtil.removeAny(dict, EXCLUDE_PROPERTIES);
|
MapUtil.removeAny(dict, EXCLUDE_PROPERTIES);
|
||||||
MapUtil.removeAny(dict, excludeParamNames);
|
MapUtil.removeAny(dict, excludeParamNames);
|
||||||
str = JsonUtils.toJsonString(dict);
|
str = JsonUtils.toSafeJsonString(dict);
|
||||||
}
|
}
|
||||||
params.add(str);
|
params.add(str);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -66,7 +66,7 @@ public class GlobalExceptionHandler {
|
|||||||
public String handleNotLoginException(SseException e, HttpServletRequest request) {
|
public String handleNotLoginException(SseException e, HttpServletRequest request) {
|
||||||
String requestURI = request.getRequestURI();
|
String requestURI = request.getRequestURI();
|
||||||
log.debug("请求地址'{}',认证失败'{}',无法访问系统资源", requestURI, e.getMessage());
|
log.debug("请求地址'{}',认证失败'{}',无法访问系统资源", requestURI, e.getMessage());
|
||||||
return JsonUtils.toJsonString(R.fail(HttpStatus.HTTP_UNAUTHORIZED, "认证失败,无法访问系统资源"));
|
return JsonUtils.toSafeJsonString(R.fail(HttpStatus.HTTP_UNAUTHORIZED, "认证失败,无法访问系统资源"));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@ -43,7 +43,7 @@ public class PlusWebInvokeTimeInterceptor implements HandlerInterceptor {
|
|||||||
} else {
|
} else {
|
||||||
Map<String, String[]> parameterMap = request.getParameterMap();
|
Map<String, String[]> parameterMap = request.getParameterMap();
|
||||||
if (MapUtil.isNotEmpty(parameterMap)) {
|
if (MapUtil.isNotEmpty(parameterMap)) {
|
||||||
String parameters = JsonUtils.toJsonString(parameterMap);
|
String parameters = JsonUtils.toSafeJsonString(parameterMap);
|
||||||
log.info("[PLUS]开始请求 => URL[{}],参数类型[param],参数:[{}]", url, parameters);
|
log.info("[PLUS]开始请求 => URL[{}],参数类型[param],参数:[{}]", url, parameters);
|
||||||
} else {
|
} else {
|
||||||
log.info("[PLUS]开始请求 => URL[{}],无参数", url);
|
log.info("[PLUS]开始请求 => URL[{}],无参数", url);
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user