Invoking java code from Enonic xp

Hi ,

I am able to invoke JAVA code from javascript but at that time i was passing only variable in function.
Now i have a arraylist of JSON object which i need to pass to java function .
How mapping will happen ?

Shall i create a setter and getters in Java with argument as Arraylist of objects and each object will be having a getter and setter of each of JSON property ?

Hi Vishal

To pass values from JavaScript to Java you can either pass it as an argument to the function called, or call setters on the Java object. Both work, but it’s usually more manageable to use setters when it’s more than 2 parameters.

There are some type conversions that are made in the call:

  • when passing a JavaScript string, the Java class should expect a Java String
  • when passing a JavaScript number, the Java class should expect a Java Long, Integer or Double
  • when passing a JavaScript array, the Java class should expect a Java List
  • when passing a JavaScript object, the Java class should expect a Java Map<String, Object>

The best is to look at some examples of code, like in the XP libraries: lib-http-client.

3 Likes