Search code examples
javajavascriptmirth

pass java object from javascript to java


Is it possible to pass objects created in Javascript to java?

for example I have a POJO called myPojo with propertys name and address

The object is created in Javascript:

var myPojo = new Packages.CreatePOJO();

myPojo.setName("Alpha");

Then I pass it to a java function

var replacer = new Packages.VelocityReplace(); replacer.replace("a template", myPojo);

The replacer class is this.

public class VelocityReplace { public static String replace(String templateStr, Object in) throws Exception { ... }

The exception generated is:

Wrapped java.lang.ClassCastException: org.mozilla.javascript.NativeObject cannot be cast to CreatePOJO

I am using Mirth Connect which is a real time interfacing engine that is providing & pushing the data to the myPojo object. It uses javascript for scripting, this is why I need to push the data from javascript to java.


Solution

  • Use JSON, probably. I'm assuming you don't want to pass functions as part of your object from JS to Java. Java JSON libraries include GSON, Jackson and others.