mirror of
https://github.com/dromara/RuoYi-Vue-Plus.git
synced 2025-09-24 07:19:46 +08:00
update 优化 转换注解 支持映射属性功能
This commit is contained in:
@ -21,9 +21,16 @@ public @interface Translation {
|
||||
|
||||
/**
|
||||
* 类型 (需与实现类上的 {@link com.ruoyi.common.translation.annotation.TranslationType} 注解type对应)
|
||||
* <p>
|
||||
* 默认取当前字段的值 如果设置了 @{@link Translation#mapper()} 则取映射字段的值
|
||||
*/
|
||||
String type();
|
||||
|
||||
/**
|
||||
* 映射字段 (如果不为空则取此字段的值)
|
||||
*/
|
||||
String mapper() default "";
|
||||
|
||||
/**
|
||||
* 其他条件 例如: 字典type(sys_user_sex)
|
||||
*/
|
||||
|
@ -1,5 +1,6 @@
|
||||
package com.ruoyi.common.translation.config;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.ruoyi.common.translation.annotation.TranslationType;
|
||||
import com.ruoyi.common.translation.core.TranslationInterface;
|
||||
import com.ruoyi.common.translation.core.handler.TranslationHandler;
|
||||
@ -24,6 +25,9 @@ public class TranslationConfig {
|
||||
@Autowired
|
||||
private List<TranslationInterface> list;
|
||||
|
||||
@Autowired
|
||||
private ObjectMapper objectMapper;
|
||||
|
||||
@PostConstruct
|
||||
public void init() {
|
||||
Map<String, TranslationInterface> map = new HashMap<>(list.size());
|
||||
@ -36,6 +40,8 @@ public class TranslationConfig {
|
||||
}
|
||||
}
|
||||
TranslationHandler.TRANSLATION_MAPPER.putAll(map);
|
||||
// todo null值处理
|
||||
// objectMapper.getSerializerProvider().setNullValueSerializer();
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -8,10 +8,10 @@ import com.fasterxml.jackson.databind.JsonSerializer;
|
||||
import com.fasterxml.jackson.databind.SerializerProvider;
|
||||
import com.fasterxml.jackson.databind.ser.ContextualSerializer;
|
||||
import com.ruoyi.common.core.utils.StringUtils;
|
||||
import com.ruoyi.common.core.utils.reflect.ReflectUtils;
|
||||
import com.ruoyi.common.translation.annotation.Translation;
|
||||
import com.ruoyi.common.translation.core.TranslationInterface;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.BeansException;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Map;
|
||||
@ -35,16 +35,15 @@ public class TranslationHandler extends JsonSerializer<Object> implements Contex
|
||||
|
||||
@Override
|
||||
public void serialize(Object value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
|
||||
try {
|
||||
TranslationInterface trans = TRANSLATION_MAPPER.get(translation.type());
|
||||
if (ObjectUtil.isNotNull(trans)) {
|
||||
String result = trans.translation(value, translation.other());
|
||||
gen.writeString(StringUtils.isNotBlank(result) ? result : value.toString());
|
||||
} else {
|
||||
gen.writeString(value.toString());
|
||||
TranslationInterface trans = TRANSLATION_MAPPER.get(translation.type());
|
||||
if (ObjectUtil.isNotNull(trans)) {
|
||||
// 如果映射字段不为空 则取映射字段的值
|
||||
if (StringUtils.isNotBlank(translation.mapper())) {
|
||||
value = ReflectUtils.invokeGetter(gen.getCurrentValue(), translation.mapper());
|
||||
}
|
||||
} catch (BeansException e) {
|
||||
log.error("数据未查到, 采用默认处理 => {}", e.getMessage());
|
||||
String result = trans.translation(value, translation.other());
|
||||
gen.writeString(StringUtils.isNotBlank(result) ? result : value.toString());
|
||||
} else {
|
||||
gen.writeString(value.toString());
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user