fix 修复 翻译模块 无法翻译实体属性值为 Null 的字段的问题
This commit is contained in:
parent
0d755d2266
commit
5cce09b5c2
@ -3,6 +3,7 @@ package com.ruoyi.common.translation.config;
|
|||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
import com.ruoyi.common.translation.annotation.TranslationType;
|
import com.ruoyi.common.translation.annotation.TranslationType;
|
||||||
import com.ruoyi.common.translation.core.TranslationInterface;
|
import com.ruoyi.common.translation.core.TranslationInterface;
|
||||||
|
import com.ruoyi.common.translation.core.handler.TranslationBeanSerializerModifier;
|
||||||
import com.ruoyi.common.translation.core.handler.TranslationHandler;
|
import com.ruoyi.common.translation.core.handler.TranslationHandler;
|
||||||
import jakarta.annotation.PostConstruct;
|
import jakarta.annotation.PostConstruct;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
@ -40,8 +41,10 @@ public class TranslationConfig {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
TranslationHandler.TRANSLATION_MAPPER.putAll(map);
|
TranslationHandler.TRANSLATION_MAPPER.putAll(map);
|
||||||
// todo null值处理
|
// 设置 Bean 序列化修改器
|
||||||
// objectMapper.getSerializerProvider().setNullValueSerializer();
|
objectMapper.setSerializerFactory(
|
||||||
|
objectMapper.getSerializerFactory()
|
||||||
|
.withSerializerModifier(new TranslationBeanSerializerModifier()));
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,29 @@
|
|||||||
|
package com.ruoyi.common.translation.core.handler;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.databind.BeanDescription;
|
||||||
|
import com.fasterxml.jackson.databind.SerializationConfig;
|
||||||
|
import com.fasterxml.jackson.databind.ser.BeanPropertyWriter;
|
||||||
|
import com.fasterxml.jackson.databind.ser.BeanSerializerModifier;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Bean 序列化修改器 解决 Null 被单独处理问题
|
||||||
|
*
|
||||||
|
* @author Lion Li
|
||||||
|
*/
|
||||||
|
public class TranslationBeanSerializerModifier extends BeanSerializerModifier {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<BeanPropertyWriter> changeProperties(SerializationConfig config, BeanDescription beanDesc,
|
||||||
|
List<BeanPropertyWriter> beanProperties) {
|
||||||
|
for (BeanPropertyWriter writer : beanProperties) {
|
||||||
|
// 如果序列化器为 TranslationHandler 的话 将 Null 值也交给他处理
|
||||||
|
if (writer.getSerializer() instanceof TranslationHandler serializer) {
|
||||||
|
writer.assignNullSerializer(serializer);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return beanProperties;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -41,10 +41,15 @@ public class TranslationHandler extends JsonSerializer<Object> implements Contex
|
|||||||
if (StringUtils.isNotBlank(translation.mapper())) {
|
if (StringUtils.isNotBlank(translation.mapper())) {
|
||||||
value = ReflectUtils.invokeGetter(gen.getCurrentValue(), translation.mapper());
|
value = ReflectUtils.invokeGetter(gen.getCurrentValue(), translation.mapper());
|
||||||
}
|
}
|
||||||
|
// 如果为 null 直接写出
|
||||||
|
if (ObjectUtil.isNull(value)) {
|
||||||
|
gen.writeNull();
|
||||||
|
return;
|
||||||
|
}
|
||||||
String result = trans.translation(value, translation.other());
|
String result = trans.translation(value, translation.other());
|
||||||
gen.writeString(StringUtils.isNotBlank(result) ? result : value.toString());
|
gen.writeString(result);
|
||||||
} else {
|
} else {
|
||||||
gen.writeString(value.toString());
|
gen.writeObject(value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -69,10 +69,16 @@ public class TestDemoVo implements Serializable {
|
|||||||
/**
|
/**
|
||||||
* 创建人
|
* 创建人
|
||||||
*/
|
*/
|
||||||
@Translation(type = TransConstant.USER_ID_TO_NAME)
|
|
||||||
@ExcelProperty(value = "创建人")
|
@ExcelProperty(value = "创建人")
|
||||||
private Long createBy;
|
private Long createBy;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 创建人账号
|
||||||
|
*/
|
||||||
|
@Translation(type = TransConstant.USER_ID_TO_NAME, mapper = "createBy")
|
||||||
|
@ExcelProperty(value = "创建人账号")
|
||||||
|
private String createByName;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 更新时间
|
* 更新时间
|
||||||
*/
|
*/
|
||||||
@ -86,5 +92,11 @@ public class TestDemoVo implements Serializable {
|
|||||||
@ExcelProperty(value = "更新人")
|
@ExcelProperty(value = "更新人")
|
||||||
private Long updateBy;
|
private Long updateBy;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 更新人账号
|
||||||
|
*/
|
||||||
|
@Translation(type = TransConstant.USER_ID_TO_NAME, mapper = "updateBy")
|
||||||
|
@ExcelProperty(value = "更新人账号")
|
||||||
|
private String updateByName;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -61,7 +61,7 @@ public class SysNoticeVo implements Serializable {
|
|||||||
* 创建人名称
|
* 创建人名称
|
||||||
*/
|
*/
|
||||||
@Translation(type = TransConstant.USER_ID_TO_NAME, mapper = "createBy")
|
@Translation(type = TransConstant.USER_ID_TO_NAME, mapper = "createBy")
|
||||||
private String createByName = "";
|
private String createByName;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 创建时间
|
* 创建时间
|
||||||
|
@ -107,13 +107,13 @@
|
|||||||
<span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
|
<span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
|
||||||
</template>
|
</template>
|
||||||
</el-table-column>
|
</el-table-column>
|
||||||
<el-table-column label="创建人" align="center" prop="createBy" />
|
<el-table-column label="创建人" align="center" prop="createByName" />
|
||||||
<el-table-column label="更新时间" align="center" prop="updateTime" width="180">
|
<el-table-column label="更新时间" align="center" prop="updateTime" width="180">
|
||||||
<template slot-scope="scope">
|
<template slot-scope="scope">
|
||||||
<span>{{ parseTime(scope.row.updateTime, '{y}-{m}-{d}') }}</span>
|
<span>{{ parseTime(scope.row.updateTime, '{y}-{m}-{d}') }}</span>
|
||||||
</template>
|
</template>
|
||||||
</el-table-column>
|
</el-table-column>
|
||||||
<el-table-column label="更新人" align="center" prop="updateBy" />
|
<el-table-column label="更新人" align="center" prop="updateByName" />
|
||||||
<el-table-column label="操作" align="center" class-name="small-padding fixed-width">
|
<el-table-column label="操作" align="center" class-name="small-padding fixed-width">
|
||||||
<template slot-scope="scope">
|
<template slot-scope="scope">
|
||||||
<el-button
|
<el-button
|
||||||
|
Loading…
x
Reference in New Issue
Block a user