开发者社区 > 云原生 > 正文

泛化调用,返回值增加的class字段,建议增加开关。

环境信息

Dubbo version: 2.6.0 Operating System version: centos Java version: 1.8

需求

泛化调用,返回值增加的class字段,建议增加开关。

举例

在PojoUtils.java中,有两处建议怎么增加该开关,如下:

1、put "class" 字段

Map<String, Object> map = new HashMap<String, Object>(); history.put(pojo, map); map.put("class", pojo.getClass().getName());

2、根据条件remove "class" 字段

// when return type is not the subclass of return type from the signature and not an interface if (!type.isInterface() && !type.isAssignableFrom(pojo.getClass())) { try { map = (Map<Object, Object>) type.newInstance(); Map<Object, Object> mapPojo = (Map<Object, Object>) pojo; map.putAll(mapPojo); map.remove("class"); } catch (Exception e) { //ignore error map = (Map<Object, Object>) pojo; } } else { map = (Map<Object, Object>) pojo; }

期待修改成以下代码:

1、put "class" 字段

Map<String, Object> map = new HashMap<String, Object>(); history.put(pojo, map); if (keepGenericClass){ // to increase the switch for control generics map.put("class", pojo.getClass().getName()); }

2、根据条件remove "class" 字段

// when return type is not the subclass of return type from the signature and not an interface if (!type.isInterface() && !type.isAssignableFrom(pojo.getClass())) { try { map = (Map<Object, Object>) type.newInstance(); Map<Object, Object> mapPojo = (Map<Object, Object>) pojo; map.putAll(mapPojo); if (keepGenericClass){ // to increase the switch for control generics map.remove("class"); } } catch (Exception e) { //ignore error map = (Map<Object, Object>) pojo; } } else { map = (Map<Object, Object>) pojo; }

原提问者GitHub用户shihuili1218

展开
收起
大圣东游 2023-05-11 20:12:34 155 0
1 条回答
写回答
取消 提交回答
  • 我将fastjson与类exclude一起使用

    /** * remove specify class field refer to dubbo generic invoke */ private static final SimplePropertyPreFilter CLASS_NAME_PRE_FILTER = new SimplePropertyPreFilter(HashMap.class);

    static {
        CLASS_NAME_PRE_FILTER.getExcludes().add("class");
    }
    

    JSON.toJSONString(result, CLASS_NAME_PRE_FILTER

    原回答者GitHub用户qixiaobo

    2023-05-12 12:11:29
    赞同 展开评论 打赏
问答地址:

阿里云拥有国内全面的云原生产品技术以及大规模的云原生应用实践,通过全面容器化、核心技术互联网化、应用 Serverless 化三大范式,助力制造业企业高效上云,实现系统稳定、应用敏捷智能。拥抱云原生,让创新无处不在。

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载