Home > Blockchain >  Mule 4 Dataweave 2 Invoke a static Java method which accepts a List of custom POJO
Mule 4 Dataweave 2 Invoke a static Java method which accepts a List of custom POJO

Time:02-06

I am trying to invoke a static method in a Java class that accepts a ArrayList of Student POJO Here is the Java code :

public class StudentService {

    public static Map<String,Student> mytest(List<Student> data) {

        for(int i=0;i<data.size();i  ) {
           // fails here 
           Student s1 = data.get(i);
          // do some processing and then store results into a Map
          
        }

        // then return this Map
        return studentMap;
    }
}

public class Student {
    
    public Student(String id, String name, String lastName) {
        super();
        this.id = id;
        this.name = name;
        this.lastName = lastName;
    }
    
    public Student() {      
    };
    
    private String id;
    private String name;
    private String lastName;

    // getters and setters
}

When the control reaches the Java method I can see that its an arraylist but rather than each element of the array being a StudentObject , it is a LinkedHashMap<String,String>

Here is the debugger view of datastructure when control reaches Java : enter image description here

I did find something similar where input json object is being converted into a Java POJO I just need to have a collection of POJO ..... So my question is how do I pass this json array to the Java method as a List ?

CodePudding user response:

This topic is actually described in the documentation: https://docs.mulesoft.com/dataweave/2.4/dataweave-formats-java#example3. Just convert the input list to the expected Java list type.

Example

output application/java
---
payload as Array {class: "java.util.ArrayList<Student>"}

You could also convert just each element inside the array but the class implementing the list would be choosen by DataWeave.

  • Related