[关闭]
@liruiyi962464 2025-04-09T00:47:16.000000Z 字数 3043 阅读 23

Jeecg-boot字典翻译改造

代码

一.找到字典切面类(DictAspect)

二.改造方法(parseDictText)

三.修改后的parseDictText方法,支持IPage、List、Object,parseDictText 注释此方法,完后拷贝下方代码

  1. private void parseDictText(Object result) {
  2. if (result instanceof Result) {
  3. List<Object> list = new LinkedList<>();
  4. if (((Result) result).getResult() instanceof IPage) {
  5. //分页
  6. list = ((IPage) ((Result) result).getResult()).getRecords();
  7. } else if (((Result) result).getResult() instanceof List) {
  8. //List集合
  9. list = (List<Object>) ((Result) result).getResult();
  10. }else{
  11. //单对象
  12. Object record = ((Result) result).getResult();
  13. //判断能否转换成JSON,因为有些结果集返回的是String类型,导致翻译异常,因此判断是否可以转换json
  14. if(checkIsJsonStr(record)){
  15. //字典翻译
  16. record = this.dictEscape(record);
  17. }
  18. ((Result) result).setResult(record);
  19. }
  20. if(list != null && list.size() > 0){
  21. List<Object> items = new ArrayList<>();
  22. for(Object record : list){
  23. if(checkIsJsonStr(record)){
  24. //字典翻译
  25. record = this.dictEscape(record);
  26. }
  27. items.add(record);
  28. }
  29. if (((Result) result).getResult() instanceof IPage) {
  30. ((IPage) ((Result) result).getResult()).setRecords(items);
  31. } else if (((Result) result).getResult() instanceof List) {
  32. ((Result) result).setResult(items);
  33. }
  34. }
  35. }
  36. }

四.提取公共代码作为单独的方法进行翻译

  1. /**
  2. * 字典翻译
  3. * @param record
  4. * @return
  5. */
  6. private JSONObject dictEscape(Object record){
  7. ObjectMapper mapper = new ObjectMapper();
  8. String json = "{}";
  9. JSONObject item = null;
  10. try {
  11. //解决@JsonFormat注解解析不了的问题详见SysAnnouncement类的@JsonFormat
  12. json = mapper.writeValueAsString(record);//对象序列化为JSON字符串
  13. } catch (JsonProcessingException e) {
  14. log.error("json解析失败" + e.getMessage(), e);
  15. }
  16. try {
  17. item = JSONObject.parseObject(json);
  18. //update-begin--Author:scott -- Date:20190603 ----for:解决继承实体字段无法翻译问题------
  19. for (Field field : oConvertUtils.getAllFields(record)) {
  20. //update-end--Author:scott -- Date:20190603 ----for:解决继承实体字段无法翻译问题------
  21. if (field.getAnnotation(Dict.class) != null) {
  22. String code = field.getAnnotation(Dict.class).dicCode();
  23. String text = field.getAnnotation(Dict.class).dicText();
  24. String table = field.getAnnotation(Dict.class).dictTable();
  25. String key = String.valueOf(item.get(field.getName()));
  26. //翻译字典值对应的txt
  27. String textValue = key;
  28. //非中文时翻译
  29. if(!checkCountName(key)){
  30. textValue = translateDictValue(code, text, table, key);
  31. }
  32. log.debug(" 字典Val : " + textValue);
  33. log.debug(" __翻译字典字段__ " + field.getName() + CommonConstant.DICT_TEXT_SUFFIX + ": " + textValue);
  34. item.put(field.getName() + CommonConstant.DICT_TEXT_SUFFIX, textValue);
  35. }
  36. //date类型默认转换string格式化日期
  37. if (field.getType().getName().equals("java.util.Date") && field.getAnnotation(JsonFormat.class) == null && item.get(field.getName()) != null) {
  38. SimpleDateFormat aDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  39. item.put(field.getName(), aDate.format(new Date((Long) item.get(field.getName()))));
  40. }
  41. }
  42. }catch (Exception e){
  43. log.info("字典翻译异常:"+e.getMessage(),e);
  44. }
  45. return item;
  46. }

五.增加中文检测方法

  1. /**
  2. * 检测是否是中文
  3. * @param countName
  4. * @return
  5. */
  6. public static boolean checkCountName(String countName){
  7. Pattern p = Pattern.compile("[\u4e00-\u9fa5]");
  8. Matcher m = p.matcher(countName);
  9. if (m.find()) {
  10. return true;
  11. }
  12. return false;
  13. }

六.增加检测是否可转换为JSON字符串方法

  1. /**
  2. * 检测是否可转换为JSON字符串
  3. * @param record
  4. * @return
  5. */
  6. public static boolean checkIsJsonStr(Object record){
  7. boolean jsonFlag = false;
  8. try {
  9. String json = new ObjectMapper().writeValueAsString(record);
  10. if(json.startsWith("{")) {
  11. jsonFlag = true;
  12. }
  13. } catch (JsonProcessingException e) {
  14. e.printStackTrace();
  15. }
  16. return jsonFlag;
  17. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注