jeecg-boot字典翻译改造(支持实体类详情查询自动翻译)

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

  2. 改造方法(parseDictText)

支持自动生成的列表接口/单个实体类查询翻译
代码如下:

private void parseDictText(Object result) {
        if (result instanceof Result) {
            if (((Result) result).getResult() instanceof IPage) {
                List<JSONObject> items = new ArrayList<>();
                for (Object record : ((IPage) ((Result) result).getResult()).getRecords()) {
                    ObjectMapper mapper = new ObjectMapper();
                    String json = "{}";
                    try {
                        //解决@JsonFormat注解解析不了的问题详见SysAnnouncement类的@JsonFormat
                        json = mapper.writeValueAsString(record);
                    } catch (JsonProcessingException e) {
                        log.error("json解析失败" + e.getMessage(), e);
                    }
                    JSONObject item = JSONObject.parseObject(json);
                    //update-begin--Author:scott -- Date:20190603 ----for:解决继承实体字段无法翻译问题------
                    //for (Field field : record.getClass().getDeclaredFields()) {
                    for (Field field : oConvertUtils.getAllFields(record)) {
                        //update-end--Author:scott  -- Date:20190603 ----for:解决继承实体字段无法翻译问题------
                        if (field.getAnnotation(Dict.class) != null) {
                            String code = field.getAnnotation(Dict.class).dicCode();
                            String text = field.getAnnotation(Dict.class).dicText();
                            String table = field.getAnnotation(Dict.class).dictTable();
                            String key = String.valueOf(item.get(field.getName()));

                            //翻译字典值对应的txt
                            String textValue = translateDictValue(code, text, table, key);

                            log.debug(" 字典Val : " + textValue);
                            log.debug(" __翻译字典字段__ " + field.getName() + CommonConstant.DICT_TEXT_SUFFIX + ": " + textValue);
                            item.put(field.getName() + CommonConstant.DICT_TEXT_SUFFIX, textValue);
                        }
                        //date类型默认转换string格式化日期
                        if (field.getType().getName().equals("java.util.Date") && field.getAnnotation(JsonFormat.class) == null && item.get(field.getName()) != null) {
                            SimpleDateFormat aDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                            item.put(field.getName(), aDate.format(new Date((Long) item.get(field.getName()))));
                        }
                    }
                    items.add(item);
                }
                ((IPage) ((Result) result).getResult()).setRecords(items);
            } else {
                Object record = (Object) ((Result) result).getResult();
                ObjectMapper mapper = new ObjectMapper();
                String json = "{}";
                try {
                    //解决@JsonFormat注解解析不了的问题详见SysAnnouncement类的@JsonFormat
                    json = mapper.writeValueAsString(record);
                } catch (JsonProcessingException e) {
                    log.error("json解析失败" + e.getMessage(), e);
                }
                try {
                    JSONObject item = JSONObject.parseObject(json);
                    //update-begin--Author:scott -- Date:20190603 ----for:解决继承实体字段无法翻译问题------
                    //for (Field field : record.getClass().getDeclaredFields()) {
                    for (Field field : oConvertUtils.getAllFields(record)) {
                        //update-end--Author:scott  -- Date:20190603 ----for:解决继承实体字段无法翻译问题------
                        if (field.getAnnotation(Dict.class) != null) {
                            String code = field.getAnnotation(Dict.class).dicCode();
                            String text = field.getAnnotation(Dict.class).dicText();
                            String table = field.getAnnotation(Dict.class).dictTable();
                            String key = String.valueOf(item.get(field.getName()));

                            //翻译字典值对应的txt
                            String textValue = translateDictValue(code, text, table, key);

                            log.debug(" 字典Val : " + textValue);
                            log.debug(" __翻译字典字段__ " + field.getName() + CommonConstant.DICT_TEXT_SUFFIX + ": " + textValue);
                            item.put(field.getName() + CommonConstant.DICT_TEXT_SUFFIX, textValue);
                        }
                        //date类型默认转换string格式化日期
                        if (field.getType().getName().equals("java.util.Date") && field.getAnnotation(JsonFormat.class) == null && item.get(field.getName()) != null) {
                            SimpleDateFormat aDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                            item.put(field.getName(), aDate.format(new Date((Long) item.get(field.getName()))));
                        }
                    }
//                }
                    ((Result) result).setResult(item);
                }catch (Exception e){
                    log.info("########################此返回类型不支持字典翻译########################");
                }
            }
        }
    }

单个查询返回数据格式样例:

/**
 * 通过id查询
 *
 * @param id
 * @return
 */

@GetMapping(value = "/queryById")
public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
   ShgBanner shgBanner = shgBannerService.getById(id);
   if(shgBanner==null) {
      return Result.error("未找到对应数据");
   }
   return Result.ok(shgBanner);
}

第二版改造 : 2023/2/22

总结:

  1. 支持单个实体类解析
  2. 支持实体类集合解析
  3. 支持过滤基本类型集合不进行解析, 暂时过滤了三种 java.lang.String java.lang.long java.lang.Integer

完整方法如下 :

private void parseDictTextOne(Object result) {
        if (result instanceof Result) {
            if (((Result) result).getResult() instanceof IPage) {
                List<JSONObject> items = new ArrayList<>();
                for (Object record : ((IPage) ((Result) result).getResult()).getRecords()) {
                    ObjectMapper mapper = new ObjectMapper();
                    String json = "{}";
                    try {
                        //解决@JsonFormat注解解析不了的问题详见SysAnnouncement类的@JsonFormat
                        json = mapper.writeValueAsString(record);
                    } catch (JsonProcessingException e) {
                        log.error("json解析失败" + e.getMessage(), e);
                    }
                    JSONObject item = JSONObject.parseObject(json);
                    //update-begin--Author:scott -- Date:20190603 ----for:解决继承实体字段无法翻译问题------
                    //for (Field field : record.getClass().getDeclaredFields()) {
                    for (Field field : oConvertUtils.getAllFields(record)) {
                        //update-end--Author:scott  -- Date:20190603 ----for:解决继承实体字段无法翻译问题------
                        if (field.getAnnotation(Dict.class) != null) {
                            String code = field.getAnnotation(Dict.class).dicCode();
                            String text = field.getAnnotation(Dict.class).dicText();
                            String table = field.getAnnotation(Dict.class).dictTable();
                            String key = String.valueOf(item.get(field.getName()));

                            //翻译字典值对应的txt
                            String textValue = translateDictValue(code, text, table, key);

                            log.debug(" 字典Val : " + textValue);
                            log.debug(" __翻译字典字段__ " + field.getName() + CommonConstant.DICT_TEXT_SUFFIX + ": " + textValue);
                            item.put(field.getName() + CommonConstant.DICT_TEXT_SUFFIX, textValue);
                        }
                        //date类型默认转换string格式化日期
                        if (field.getType().getName().equals("java.util.Date") && field.getAnnotation(JsonFormat.class) == null && item.get(field.getName()) != null) {
                            SimpleDateFormat aDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                            item.put(field.getName(), aDate.format(new Date((Long) item.get(field.getName()))));
                        }
                    }
                    items.add(item);
                }
                ((IPage) ((Result) result).getResult()).setRecords(items);
            } else if (((Result) result).getResult() instanceof List) {
                List result1 = (List) ((Result) result).getResult();
                if (CollectionUtils.isNotEmpty(result1)) {
                    Class<?> aClass = result1.get(0).getClass();
                    String className = aClass.getName();
                    log.info("------- List<T> 泛型 T is {}", className);
                    //只解析result中list泛型是object的数据, 基本数据类型不解析
                    /**
                     * 以下类型不解析
                     * java.lang.String
                     * java.lang.long
                     * java.lang.Integer
                     */
                    if (!className.equals("java.lang.String") && !className.equals("java.lang.long") && !className.equals("java.lang.Integer")) {
                        List<JSONObject> items = new ArrayList<>();
                        for (Object record : (List) ((Result) result).getResult()) {
                            ObjectMapper mapper = new ObjectMapper();
                            String json = "{}";
                            try {
                                //解决@JsonFormat注解解析不了的问题详见SysAnnouncement类的@JsonFormat
                                json = mapper.writeValueAsString(record);
                            } catch (JsonProcessingException e) {
                                log.error("json解析失败" + e.getMessage(), e);
                            }
                            JSONObject item = JSONObject.parseObject(json);
                            //update-begin--Author:scott -- Date:20190603 ----for:解决继承实体字段无法翻译问题------
                            //for (Field field : record.getClass().getDeclaredFields()) {
                            for (Field field : oConvertUtils.getAllFields(record)) {
                                //update-end--Author:scott  -- Date:20190603 ----for:解决继承实体字段无法翻译问题------
                                if (field.getAnnotation(Dict.class) != null) {
                                    String code = field.getAnnotation(Dict.class).dicCode();
                                    String text = field.getAnnotation(Dict.class).dicText();
                                    String table = field.getAnnotation(Dict.class).dictTable();
                                    String key = String.valueOf(item.get(field.getName()));

                                    //翻译字典值对应的txt
                                    String textValue = translateDictValue(code, text, table, key);

                                    log.debug(" 字典Val : " + textValue);
                                    log.debug(" __翻译字典字段__ " + field.getName() + CommonConstant.DICT_TEXT_SUFFIX + ": " + textValue);
                                    item.put(field.getName() + CommonConstant.DICT_TEXT_SUFFIX, textValue);
                                }
                                //date类型默认转换string格式化日期
                                if (field.getType().getName().equals("java.util.Date") && field.getAnnotation(JsonFormat.class) == null && item.get(field.getName()) != null) {
                                    SimpleDateFormat aDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                                    item.put(field.getName(), aDate.format(new Date((Long) item.get(field.getName()))));
                                }
                            }
                            items.add(item);
                        }
                        ((Result) result).setResult(items);
                    }else {
                        log.info("只解析result中list<T>泛型是实体类的数据, 基本数据类型不解析: T is {}", className);
                    }
                }else {
                    log.info(" result中List<T> 为空,跳过不解析 ");
                }

            } else {
                Object record = (Object) ((Result) result).getResult();
                ObjectMapper mapper = new ObjectMapper();
                String json = "{}";
                try {
                    //解决@JsonFormat注解解析不了的问题详见SysAnnouncement类的@JsonFormat
                    json = mapper.writeValueAsString(record);
                } catch (JsonProcessingException e) {
                    log.error("json解析失败" + e.getMessage(), e);
                }
                try {
                    JSONObject item = JSONObject.parseObject(json);
                    //update-begin--Author:scott -- Date:20190603 ----for:解决继承实体字段无法翻译问题------
                    //for (Field field : record.getClass().getDeclaredFields()) {
                    for (Field field : oConvertUtils.getAllFields(record)) {
                        //update-end--Author:scott  -- Date:20190603 ----for:解决继承实体字段无法翻译问题------
                        if (field.getAnnotation(Dict.class) != null) {
                            String code = field.getAnnotation(Dict.class).dicCode();
                            String text = field.getAnnotation(Dict.class).dicText();
                            String table = field.getAnnotation(Dict.class).dictTable();
                            String key = String.valueOf(item.get(field.getName()));

                            //翻译字典值对应的txt
                            String textValue = translateDictValue(code, text, table, key);

                            log.debug(" 字典Val : " + textValue);
                            log.debug(" __翻译字典字段__ " + field.getName() + CommonConstant.DICT_TEXT_SUFFIX + ": " + textValue);
                            item.put(field.getName() + CommonConstant.DICT_TEXT_SUFFIX, textValue);
                        }
                        //date类型默认转换string格式化日期
                        if (field.getType().getName().equals("java.util.Date") && field.getAnnotation(JsonFormat.class) == null && item.get(field.getName()) != null) {
                            SimpleDateFormat aDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                            item.put(field.getName(), aDate.format(new Date((Long) item.get(field.getName()))));
                        }
                    }
                    ((Result) result).setResult(item);
                } catch (Exception e) {
                    log.info("########################此返回类型不支持字典翻译########################");
                }
            }
        }
    }

改造部分如下 :

else if (((Result) result).getResult() instanceof List) {
                List result1 = (List) ((Result) result).getResult();
                if (CollectionUtils.isNotEmpty(result1)) {
                    Class<?> aClass = result1.get(0).getClass();
                    String className = aClass.getName();
                    log.info("------- List<T> 泛型 T is {}", className);
                    //只解析result中list泛型是object的数据, 基本数据类型不解析
                    /**
                     * 以下类型不解析
                     * java.lang.String
                     * java.lang.long
                     * java.lang.Integer
                     */
                    if (!className.equals("java.lang.String") && !className.equals("java.lang.long") && !className.equals("java.lang.Integer")) {
                        List<JSONObject> items = new ArrayList<>();
                        for (Object record : (List) ((Result) result).getResult()) {
                            ObjectMapper mapper = new ObjectMapper();
                            String json = "{}";
                            try {
                                //解决@JsonFormat注解解析不了的问题详见SysAnnouncement类的@JsonFormat
                                json = mapper.writeValueAsString(record);
                            } catch (JsonProcessingException e) {
                                log.error("json解析失败" + e.getMessage(), e);
                            }
                            JSONObject item = JSONObject.parseObject(json);
                            //update-begin--Author:scott -- Date:20190603 ----for:解决继承实体字段无法翻译问题------
                            //for (Field field : record.getClass().getDeclaredFields()) {
                            for (Field field : oConvertUtils.getAllFields(record)) {
                                //update-end--Author:scott  -- Date:20190603 ----for:解决继承实体字段无法翻译问题------
                                if (field.getAnnotation(Dict.class) != null) {
                                    String code = field.getAnnotation(Dict.class).dicCode();
                                    String text = field.getAnnotation(Dict.class).dicText();
                                    String table = field.getAnnotation(Dict.class).dictTable();
                                    String key = String.valueOf(item.get(field.getName()));

                                    //翻译字典值对应的txt
                                    String textValue = translateDictValue(code, text, table, key);

                                    log.debug(" 字典Val : " + textValue);
                                    log.debug(" __翻译字典字段__ " + field.getName() + CommonConstant.DICT_TEXT_SUFFIX + ": " + textValue);
                                    item.put(field.getName() + CommonConstant.DICT_TEXT_SUFFIX, textValue);
                                }
                                //date类型默认转换string格式化日期
                                if (field.getType().getName().equals("java.util.Date") && field.getAnnotation(JsonFormat.class) == null && item.get(field.getName()) != null) {
                                    SimpleDateFormat aDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                                    item.put(field.getName(), aDate.format(new Date((Long) item.get(field.getName()))));
                                }
                            }
                            items.add(item);
                        }
                        ((Result) result).setResult(items);
                    }else {
                        log.info("只解析result中list<T>泛型是实体类的数据, 基本数据类型不解析: T is {}", className);
                    }
                }else {
                    log.info(" result中List<T> 为空,跳过不解析 ");
                }

            } else {
                Object record = (Object) ((Result) result).getResult();
                ObjectMapper mapper = new ObjectMapper();
                String json = "{}";
                try {
                    //解决@JsonFormat注解解析不了的问题详见SysAnnouncement类的@JsonFormat
                    json = mapper.writeValueAsString(record);
                } catch (JsonProcessingException e) {
                    log.error("json解析失败" + e.getMessage(), e);
                }
                try {
                    JSONObject item = JSONObject.parseObject(json);
                    //update-begin--Author:scott -- Date:20190603 ----for:解决继承实体字段无法翻译问题------
                    //for (Field field : record.getClass().getDeclaredFields()) {
                    for (Field field : oConvertUtils.getAllFields(record)) {
                        //update-end--Author:scott  -- Date:20190603 ----for:解决继承实体字段无法翻译问题------
                        if (field.getAnnotation(Dict.class) != null) {
                            String code = field.getAnnotation(Dict.class).dicCode();
                            String text = field.getAnnotation(Dict.class).dicText();
                            String table = field.getAnnotation(Dict.class).dictTable();
                            String key = String.valueOf(item.get(field.getName()));

                            //翻译字典值对应的txt
                            String textValue = translateDictValue(code, text, table, key);

                            log.debug(" 字典Val : " + textValue);
                            log.debug(" __翻译字典字段__ " + field.getName() + CommonConstant.DICT_TEXT_SUFFIX + ": " + textValue);
                            item.put(field.getName() + CommonConstant.DICT_TEXT_SUFFIX, textValue);
                        }
                        //date类型默认转换string格式化日期
                        if (field.getType().getName().equals("java.util.Date") && field.getAnnotation(JsonFormat.class) == null && item.get(field.getName()) != null) {
                            SimpleDateFormat aDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                            item.put(field.getName(), aDate.format(new Date((Long) item.get(field.getName()))));
                        }
                    }
                    ((Result) result).setResult(item);
                } catch (Exception e) {
                    log.info("########################此返回类型不支持字典翻译########################");
                }
            }

支持如下返回方式 :

// 单个实体解析
@GetMapping(value = "/queryById")
public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
   ShgBanner shgBanner = shgBannerService.getById(id);
   if(shgBanner==null) {
      return Result.error("未找到对应数据");
   }
   return Result.ok(shgBanner);
}

// 列表实体解析
@GetMapping("/queryRawMaters")
public Result<?> queryRawMaters(){
	List<HgRawMaterial> rawMaterialList = hgFinishedProductSonService.queryRawMaters();
	return Result.OK(rawMaterialList);
}
  • 6
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论
### 回答1: 基于J2EE的快速开发平台Jeecg-Boot,可以帮助开发人员快速地构建和部署企业级应用程序。Jeecg-Boot使用了J2EE开发框架,如Spring Boot、MyBatis等,提供了一套全面的工具和模块,帮助开发人员简化开发过程,提高开发效率。 首先,在使用Jeecg-Boot开发时,我们可以利用其提供的代码生成工具,快速生成大量基础代码。通过定义数据表结构,代码生成工具会自动生成与数据库交互的基础增删改查模块,减少了手动编写这些重复代码的工作量,提高了开发效率。 其次,Jeecg-Boot提供了丰富的业务模块和组件,包括权限管理、菜单管理、数据字典、文件上传下载等等。这些模块和组件可以直接集成到应用程序中,减少了开发人员自行开发这些基础功能的时间和精力,同时保证了应用程序的功能完整性。 此外,Jeecg-Boot还提供了一系列的插件和扩展,可以满足不同开发需求。例如,Jeecg-Boot支持在线开发模式,在不停服的情况下,实时修改代码并生效,极大地提高了调试和修改的效率。同时,Jeecg-Boot支持分布式部署和集群部署,可以应对高并发和大规模访问的需求。 总之,基于J2EE快速开发平台Jeecg-Boot开发的优势在于提供了一套完整的开发工具和模块,帮助开发人员快速构建和部署企业级应用程序,大大提高了开发效率和质量。 ### 回答2: jeecg-boot是一个基于j2ee的快速开发平台,提供了一整套开发工具和框架,使开发者能够快速构建基于j2ee的应用程序。 jeecg-boot具有丰富的功能和特性,包括代码生成器、权限管理、后台管理、前后端分离、多数据源支持等。借助于代码生成器,开发者可以根据数据库表结构自动生成实体类、Dao、Service等代码,节省了手动编写重复代码的时间和工作量。权限管理模块可以帮助开发者实现灵活的用户权限控制,保护系统安全。后台管理模块提供了丰富的功能页面和交互界面,使开发者能够方便地管理系统。前后端分离的特性使得前端开发和后端开发可以独立进行,提高了开发效率。多数据源支持可以满足多数据库连接的需求,适用于复杂的业务场景。 在使用jeecg-boot进行开发时,开发者可以按照自己的需求进行定制和扩展。jeecg-boot提供了丰富的插件和可扩展的接口,使开发者可以灵活地集成自己的业务逻辑和功能。此外,jeecg-boot还提供了详细的文档和示例代码,方便开发者学习和使用。 总之,jeecg-boot是一个强大的基于j2ee的快速开发平台,提供了丰富的功能和特性,可以帮助开发者快速构建高质量的j2ee应用程序。无论是开发一个简单的小型应用还是一个复杂的企业级应用,jeecg-boot都是一个优秀的选择。 ### 回答3: jeecg-boot是基于J2EE的快速开发平台,它提供了一套简单、高效的开发框架,可以帮助开发人员快速构建企业级应用程序。 首先,jeecg-boot采用了主流的J2EE技术栈,包括Spring Boot、Spring Cloud、Mybatis等,这些技术在企业应用开发中被广泛应用,具有成熟、稳定的特点。通过使用这些技术,开发人员可以快速搭建整体项目架构,减少重复性的工作,提高开发效率。 其次,jeecg-boot提供了丰富的代码生成器和模板,可以根据数据库表结构自动生成代码。这样一来,开发人员只需要定义好数据库表结构,就能够生成对应的实体类、控制器、服务类等代码,省去了大量的手动编写代码的时间和精力。 此外,jeecg-boot还内置了权限管理、数据字典、代码生成、报表打印等常用功能模块,开发人员可以快速集成这些功能,并根据实际需求进行定制。同时,它还支持多数据源配置,能够满足复杂业务场景下的需求。 最后,jeecg-boot具有良好的可扩展性和可维护性。通过遵循一些设计原则和开发规范,开发人员可以编写易于扩展和维护的代码。此外,jeecg-boot还提供了一些常见的工具类和插件,帮助开发人员更好地进行开发工作。 总之,基于jeecg-boot开发可以大大提高开发效率和代码质量,快速构建功能完善、稳定可靠的企业级应用程序。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

微微一笑满城空

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值