update 优化 翻译处理器 避免多次序列化 实现类增加缓存避免重复解析

This commit is contained in:
疯狂的狮子Li
2026-03-31 18:00:01 +08:00
parent eb850fb8cf
commit 26464c0051
3 changed files with 22 additions and 30 deletions

View File

@@ -129,7 +129,7 @@ public class JsonValueEnhancer {
return jsonMapper.valueToTree(value);
}
try {
ObjectNode objectNode = asObjectNode(jsonMapper.valueToTree(value));
ObjectNode objectNode = jsonMapper.createObjectNode();
for (PropertyMetadata metadata : getProperties(value.getClass())) {
Object originalValue = metadata.getValue(value);
JsonFieldContext fieldContext = new JsonFieldContext(value, metadata.propertyName(), metadata.member(), originalValue);
@@ -158,13 +158,6 @@ public class JsonValueEnhancer {
return enhanceTree(value);
}
private ObjectNode asObjectNode(JsonNode node) {
if (node instanceof ObjectNode objectNode) {
return objectNode;
}
return jsonMapper.createObjectNode();
}
private List<PropertyMetadata> getProperties(Class<?> type) {
return propertyCache.computeIfAbsent(type, this::resolveProperties);
}

View File

@@ -2,13 +2,12 @@ package org.dromara.common.sensitive.handler;
import cn.hutool.core.util.ObjectUtil;
import lombok.extern.slf4j.Slf4j;
import org.dromara.common.core.utils.SpringUtils;
import org.dromara.common.json.enhance.JsonEnhancementContext;
import org.dromara.common.json.enhance.JsonFieldContext;
import org.dromara.common.json.enhance.JsonFieldProcessor;
import org.dromara.common.sensitive.annotation.Sensitive;
import org.dromara.common.sensitive.core.SensitiveService;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
/**
@@ -18,22 +17,19 @@ import org.springframework.core.annotation.Order;
@Order(100)
public class SensitiveJsonFieldProcessor implements JsonFieldProcessor {
@Autowired(required = false)
private SensitiveService sensitiveService;
@Override
public Object process(JsonFieldContext fieldContext, Object value, JsonEnhancementContext context) {
Sensitive sensitive = fieldContext.getAnnotation(Sensitive.class);
if (sensitive == null || !(value instanceof String text)) {
return value;
}
try {
SensitiveService sensitiveService = SpringUtils.getBean(SensitiveService.class);
if (ObjectUtil.isNotNull(sensitiveService) && sensitiveService.isSensitive(sensitive.roleKey(), sensitive.perms())) {
return sensitive.strategy().desensitizer().apply(text);
}
return text;
} catch (BeansException e) {
log.error("脱敏实现不存在, 采用默认处理 => {}", e.getMessage());
return text;
if (ObjectUtil.isNotNull(sensitiveService) && sensitiveService.isSensitive(sensitive.roleKey(), sensitive.perms())) {
return sensitive.strategy().desensitizer().apply(text);
}
return text;
}
}

View File

@@ -1,7 +1,6 @@
package org.dromara.common.translation.core.handler;
import cn.hutool.core.util.ObjectUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.core.utils.reflect.ReflectUtils;
@@ -13,11 +12,11 @@ import org.dromara.common.translation.annotation.TranslationType;
import org.dromara.common.translation.core.TranslationInterface;
import org.springframework.core.annotation.Order;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
/**
@@ -25,14 +24,24 @@ import java.util.Set;
*/
@Slf4j
@Order(0)
@RequiredArgsConstructor
public class TranslationJsonFieldProcessor implements JsonFieldProcessor {
private static final String ATTR_BATCHES = TranslationJsonFieldProcessor.class.getName() + ".batches";
private static final String ATTR_RESULTS = TranslationJsonFieldProcessor.class.getName() + ".results";
private final List<TranslationInterface<?>> translations;
private final Map<String, TranslationInterface<?>> translationMap;
public TranslationJsonFieldProcessor(List<TranslationInterface<?>> translations) {
Map<String, TranslationInterface<?>> map = new LinkedHashMap<>(translations.size());
for (TranslationInterface<?> t : translations) {
TranslationType annotation = t.getClass().getAnnotation(TranslationType.class);
if (annotation != null) {
map.put(annotation.type(), t);
}
}
this.translationMap = Collections.unmodifiableMap(map);
}
@Override
public void collect(JsonFieldContext fieldContext, JsonEnhancementContext context) {
@@ -118,13 +127,7 @@ public class TranslationJsonFieldProcessor implements JsonFieldProcessor {
}
private TranslationInterface<?> getTranslation(String type) {
for (TranslationInterface<?> translation : translations) {
TranslationType translationType = translation.getClass().getAnnotation(TranslationType.class);
if (translationType != null && Objects.equals(type, translationType.type())) {
return translation;
}
}
return null;
return translationMap.get(type);
}
private record TranslationBatchKey(String type, String other) {