ravibeli
ravibeli

Reputation: 494

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

I am working on Mule 3.9 to 4.1.4 migration work, I have java logic called in global functions defined by Groovy script in global-config.xml in Mule 3.9, trying to migrate it in Mule 4 with below approach.

Here is one use case, where java static method takes Map as an argument, in Dataweave 2.0 I did not see any example where Dataweave calling java method having Map object. Since tried below option

Option-1: Existing code

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;
    }
}

After struggling an option-1 lost a lot of time, thought to try option-2 by passing JSON string to java method then convert it to Map, then reuse the existing logic. But no luck, some other issues see error logs for more details.

Please suggest me if there is any solution for it???

Option-2: Existing code

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;
    }
}

But having some challenges here too, I have Gson library as part of APIKit mule module, I tried to add a Gson dependency in the inclusion list in pom, also adding in sharedLibrary, still no luck :(

Error log:

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)
}

Upvotes: 0

Views: 1891

Answers (1)

Ryan Carter
Ryan Carter

Reputation: 11606

For the first part you can pass the map to that method by coercing to an object. Based on your class, this works for me:

%dw 2.0

import java!com::mycompany::JsonUtil

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

For part 2: I'm guessing is not needed if option 1 works. However you need to add the dependency specifically to you app pom is all and do not rely on transitive dependencies. That is best practice anyway as you cannot expect APIKit to always use gson:

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

Upvotes: 2

Related Questions