我正在处理Mule 3.9到4.1.4的迁移工作,Groovy脚本在Mule 3.9中的glob-config.xml中定义的全局函数中调用了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;
}
}在苦苦挣扎之后,option-1损失了很多时间,考虑通过将JSON字符串传递给java方法,然后将其转换为Map,然后重用现有的逻辑来尝试option-2。但是没有运气,其他一些问题请参阅错误日志以获得更多详细信息。
,如果有什么解决办法,请给我建议?
选项-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骡子模块的一部分,我尝试在pom中的包含列表中添加Gson依赖项,也在sharedLibrary中添加了Gson依赖项,但仍然没有成功:
错误日志:
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)
}发布于 2019-03-20 18:04:28
对于第一部分,您可以通过胁迫对象将映射传递给该方法。根据你们班的情况,这对我来说很管用:
%dw 2.0
import java!com::mycompany::JsonUtil
var mymap = {key:"val"}
output application/json
---
{
result: JsonUtil::getTableAndColumns(mymap as Object)
}对于第2部分:我猜如果选项1有效,就不需要了。但是,您需要将依赖项专门添加到您的app pom中,并且不依赖于传递依赖关系。这是最佳实践,因为您不能期望APIKit总是使用gson:
<dependencies>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.5</version>
</dependency>
...
</dependencies>https://stackoverflow.com/questions/55261741
复制相似问题