繁体   English   中英

Mule-4:在Dataweave 2.0中使用Map对象作为参数的静态方法调用

[英]Mule-4: static method call with Map object as argument in Dataweave 2.0

我正在将Mule 3.9迁移到4.1.4,我在Mule 3.9的global-config.xml中由Groovy脚本定义的全局函数中调用了Java逻辑,试图通过以下方法在Mule 4中进行迁移。

这是一个用例,其中java静态方法将Map作为参数,在Dataweave 2.0中,我没有看到Dataweave调用具有Map对象的java方法的任何示例。 由于尝试以下选项

选项1:现有代码

class JsonUtil {
    public static List<Map<String, String>> getTableAndColumns(Map<String, Object> inputJsonMap) {
        List<Map<String, String>> list = null;
        //Lot of big logic that to get list out of input Map object
        return list;
    }
}

在选择1挣扎之后,浪费了很多时间,可以考虑通过将JSON字符串传递给java方法来尝试选择2,然后将其转换为Map,然后重用现有逻辑。 但是不走运,其他一些问题请参见错误日志以获取更多详细信息。

请建议我是否有解决方案???

选项2:现有代码

class JsonUtil {
    public static List<Map<String, String>> getTableAndColumns(String inputJsonStr) {
        //Using my own utility class to convert JSON string to Map
        Map<String, Object> inputJsonMap = MyUtil.toMap(inputJsonStr, Map.class)
        List<Map<String, String>> list = null;
        //Lot of big logic that to get list out of input Map object
        return list;
    }
}

但是这里也有一些挑战,我将Gson库作为APIKit ule模块的一部分,我试图在pom的包含列表中添加Gson依赖项,还添加了sharedLibrary,仍然没有运气:(

错误日志:

An exception occurred while trying to execute function  `com.mycompany.JsonUtil.getTableAndColumns(java.lang.String)`.
Caused by: java.lang.NoClassDefFoundError: com/google/gson/Gson
Unknown location
Trace:
  at invoke (line: -1, column: -1)
  at getTableAndColumns (line: -1, column: -1)
  at main (line: 9, column: 16)" evaluating expression: "%dw 2.0
import java!com::mycompany::util::JsonUtil
output application/json

---
{
    table_column: StringUtil::getTableAndColumns(vars.inputJson)
}

对于第一部分,您可以通过强制对象来将映射传递给该方法。 根据您的课程,这对我有用:

%dw 2.0

import java!com::mycompany::JsonUtil

var mymap = {key:"val"}
output application/json
---
{
    result: JsonUtil::getTableAndColumns(mymap as Object)
}

对于第2部分:我猜想如果选项1有效,则不需要。 但是,您需要将依赖项专门添加到您的应用程序中,而pom全部不依赖于传递性依赖项。 无论如何,这都是最佳实践,因为您不能期望APIKit始终使用gson:

<dependencies>
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.8.5</version>
        </dependency>
...
</dependencies>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM