update 优化 翻译组件 支持返回值泛型 支持多种类型数据翻译(例如: 根据主键翻译成对象)

2.X
疯狂的狮子Li 2 years ago
parent baa00ba452
commit 331238a057

@ -24,15 +24,15 @@ import java.util.Map;
public class TranslationConfig {
@Autowired
private List<TranslationInterface> list;
private List<TranslationInterface<?>> list;
@Autowired
private ObjectMapper objectMapper;
@PostConstruct
public void init() {
Map<String, TranslationInterface> map = new HashMap<>(list.size());
for (TranslationInterface trans : list) {
Map<String, TranslationInterface<?>> map = new HashMap<>(list.size());
for (TranslationInterface<?> trans : list) {
if (trans.getClass().isAnnotationPresent(TranslationType.class)) {
TranslationType annotation = trans.getClass().getAnnotation(TranslationType.class);
map.put(annotation.type(), trans);

@ -5,7 +5,7 @@ package com.ruoyi.common.translation.core;
*
* @author Lion Li
*/
public interface TranslationInterface {
public interface TranslationInterface<T> {
/**
*
@ -13,5 +13,5 @@ public interface TranslationInterface {
* @param key ()
* @return
*/
String translation(Object key, String other);
T translation(Object key, String other);
}

@ -29,13 +29,13 @@ public class TranslationHandler extends JsonSerializer<Object> implements Contex
/**
*
*/
public static final Map<String, TranslationInterface> TRANSLATION_MAPPER = new ConcurrentHashMap<>();
public static final Map<String, TranslationInterface<?>> TRANSLATION_MAPPER = new ConcurrentHashMap<>();
private Translation translation;
@Override
public void serialize(Object value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
TranslationInterface trans = TRANSLATION_MAPPER.get(translation.type());
TranslationInterface<?> trans = TRANSLATION_MAPPER.get(translation.type());
if (ObjectUtil.isNotNull(trans)) {
// 如果映射字段不为空 则取映射字段的值
if (StringUtils.isNotBlank(translation.mapper())) {
@ -46,8 +46,8 @@ public class TranslationHandler extends JsonSerializer<Object> implements Contex
gen.writeNull();
return;
}
String result = trans.translation(value, translation.other());
gen.writeString(result);
Object result = trans.translation(value, translation.other());
gen.writeObject(result);
} else {
gen.writeObject(value);
}

@ -16,7 +16,7 @@ import org.springframework.stereotype.Component;
@Component
@AllArgsConstructor
@TranslationType(type = TransConstant.DEPT_ID_TO_NAME)
public class DeptNameTranslationImpl implements TranslationInterface {
public class DeptNameTranslationImpl implements TranslationInterface<String> {
@DubboReference
private RemoteDeptService remoteDeptService;

@ -16,7 +16,7 @@ import org.springframework.stereotype.Component;
@Component
@AllArgsConstructor
@TranslationType(type = TransConstant.DICT_TYPE_TO_LABEL)
public class DictTypeTranslationImpl implements TranslationInterface {
public class DictTypeTranslationImpl implements TranslationInterface<String> {
private final DictService dictService;

@ -16,7 +16,7 @@ import org.springframework.stereotype.Component;
@Component
@AllArgsConstructor
@TranslationType(type = TransConstant.OSS_ID_TO_URL)
public class OssUrlTranslationImpl implements TranslationInterface {
public class OssUrlTranslationImpl implements TranslationInterface<String> {
@DubboReference
private RemoteFileService ossService;

@ -16,7 +16,7 @@ import org.springframework.stereotype.Component;
@Component
@AllArgsConstructor
@TranslationType(type = TransConstant.USER_ID_TO_NAME)
public class UserNameTranslationImpl implements TranslationInterface {
public class UserNameTranslationImpl implements TranslationInterface<String> {
@DubboReference
private RemoteUserService remoteUserService;

Loading…
Cancel
Save