参考文献:https://my.oschina.net/angelbo/blog/2875887
感谢博主提供的解决思路方法
博主缺点:不能对字符串字典进行处理,已优化
@TranslationDict({@DictParam(dictCode = "USER_GENDER_CODE",dictValueFiled = "gender",dictNameFiled = "genderName")})
package com.epf.dict;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* 翻译字典值注解
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(value={ElementType.METHOD})
@Documented
public @interface TranslationDict {
DictParam[] value();
}
package com.epf.dict;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* 需要翻译的字典值
*
* */
@Target(value={ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface DictParam {
/**
* 字典CODE
* @return
*/
String dictCode() default "";
/**
* 需要翻译的字段名
* @return
*/
String dictValueFiled() default "";
/**
* 被翻译的字段名
* @return
*/
String dictNameFiled() default "";
}
package com.epf.dict.aspect;
import static com.alibaba.fastjson.JSON.toJSONString;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.commons.lang.StringUtils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.epf.dict.DictParam;
import com.epf.dict.TranslationDict;
import com.tiaim.system.entity.DictionariesEntity;
import com.tiaim.system.service.DictionariesService;
/**
* 翻译字典值的
*/
@Aspect
@Component
public class TranslationDictAspect {
@Autowired
DictionariesService dictionariesService ;
/**
* 非基本类型在 CLASS 中的定义
*/
private static final String FILED_NAME_TYPE = "TYPE";
private Map dictInfoMap = new ConcurrentHashMap<>();
@Around("@annotation(translationDict)")
public Object Translation(final ProceedingJoinPoint pjp, TranslationDict translationDict) throws Throwable {
Object result = pjp.proceed();
// 第一步、获取返回值类型
Class returnType = ((MethodSignature) pjp.getSignature()).getReturnType();
//首先,取出要翻译字段的字典值
String returnJsonResult = toJSONString(result,SerializerFeature.WriteMapNullValue);
DictParam[] dictParams = translationDict.value();
for (DictParam dictParam : dictParams) {
//TODO 后期需优化读取Redis
List dictInfos = dictionariesService.getDictionariesListNoPage();
//先把字典值转成map
for (DictionariesEntity dictInfo : dictInfos) {
dictInfoMap.put(dictInfo.getDictKey(),dictInfo.getZhCn());
}
Pattern dictPattern= Pattern.compile("\"" + dictParam.dictValueFiled() +".*?,");
Matcher dictMatcher=dictPattern.matcher(returnJsonResult);
StringBuffer sb = new StringBuffer();
while (dictMatcher.find()){
//取出要翻译字段对应的值
Pattern dictValuePattern= Pattern.compile(":\"(.*?)\",");
Matcher dictValueMatcher = dictValuePattern.matcher(dictMatcher.group().toString());
if( dictValueMatcher.find()){
//翻译字典
String dictInfoName = dictInfoMap.get(dictValueMatcher.group(1));
String s = dictMatcher.group() + "\"" + dictParam.dictNameFiled() + "\":\"" +dictInfoName + "\",";
dictMatcher.appendReplacement(sb, s);
}else{
String s = dictMatcher.group() + "\"" + dictParam.dictNameFiled() + "\":\"\",";
dictMatcher.appendReplacement(sb, s);
}
}
dictMatcher.appendTail(sb);
returnJsonResult = sb.toString();
}
result = getJsonToResultObject(returnJsonResult,returnType);
return result;
}
private Object getJsonToResultObject(String returnJsonResult, Class returnType) {
// 对象复原
if (StringUtils.isNotBlank(returnJsonResult)) {
// 基本类型则直接返回封装类型则对象转换
if (returnType.isPrimitive()) {
return returnJsonResult;
} else {
try {
if ((returnType.getField(FILED_NAME_TYPE)
.getClass()).isPrimitive()) {
return returnJsonResult;
}
} catch (Exception e) {
return JSON.parseObject(returnJsonResult, returnType);
}
}
}
return null;
}
}
拦截器主要逻辑:拦截方法,把返回值转成JSON,然后根据注解翻译JSON, 从JSON中取出DictParam中的 dictValueFiled字段的值,然后根据DictParam中的dictCode翻译对一个的字典,然后将翻译好的值插入dictNameFiled这个字段
{userId:1,geneder:1,userName:'zhangsan'}
@TranslationDict({@DictParam(dictCode = "USER_GENDER_CODE",dictValueFiled = "gender",dictNameFiled = "genderName")})
{userId:1,geneder:1,genderName:'男性',userName:'zhangsan'}
@Override
@TranslationDict({
@DictParam(dictCode = "landType",dictValueFiled = "landType",dictNameFiled = "landTypeName"),
@DictParam(dictCode = "firstClassUse",dictValueFiled = "firstClassUse",dictNameFiled = "firstClassUseName")
})
public List getTSellList(Pager pager, Example example) {
// 分页属性设置
PageHelper.startPage(pager.getPageNo(), pager.getPageSize());
List list = tSellDao.selectByExample(example);
PageInfo page = new PageInfo(list);
/**
* 通过反射设置pager相关值,并在控制层获取。
**/
pager.setTotalPage(page.getPages());
pager.setTotalRecord((int) page.getTotal());
return list;
}