1

I try to marshall some JAXB objects in Rhino Javascript. Those JAXB Objects (top root is MyClass) are created from WSDL with wsimport. The Java side of the application doesn't know about MyClass.

My rhino script looks like this :

importPackage(Packages.javax.xml.bind);
importPackage(Packages.javax.xml.namespace);
...
  var myObj = new MyClass(); // MyClass has been generated from WSDL with wsimport
  var jaxbContext = JAXBContext.newInstance(myObj.getClass().getPackage().getName());
  var marshaller = jaxbContext.createMarshaller();
  marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
  var strWriter = new StringWriter();
  var qName = new QName("xxx", "MyClass");
  var jaxbElement = new JAXBElement(qName, myObj.getClass(), myObj);
  marshaller.marshal(jaxbElement, strWriter);

Unfortunately it gives a error saying the constructor of javax.xml.bind.JAXBElement is not found for arguments object, java.lang.class, MyClass.

I also tried without constructing the jaxbElement but marshaller launch an exception saying it cannot marshall due to lack of XMLRootElement is missing.

Is there a way either to indicate to rhino the type of JAXBElement or maybe a Java code that can be called WITHOUT knowing MyClass ?

The line causing the problem is:

var jaxbElement = new JAXBElement(qName, pspApp.getClass(), pspApp);

Henrik Aasted Sørensen
  • 6,966
  • 11
  • 51
  • 60
TrapII
  • 2,219
  • 1
  • 15
  • 15
  • from what dependency are using `JAXBElement`? you might use a wrong import – Dmitry Senkovich Feb 01 '18 at 15:03
  • don't think it is an import issue. This is really related to creating an instance of a Generic with a class in JS. – TrapII Feb 01 '18 at 15:21
  • oh, really, I see, ok, just a shot in the dark: https://stackoverflow.com/a/12626143/5185646. might it be helpful? maybe it can be rewritten in JS somehow? – Dmitry Senkovich Feb 01 '18 at 15:33
  • Try inlining the `qName` variable creation expression into 1st argument of the `JAXBElement` constructor expression. The interpreter can't find a constructor for `Object, Class, Object` tuple, because such constructor doesn't exist. The type of the first argument must be `QName`, see https://docs.oracle.com/javaee/7/api/javax/xml/bind/JAXBElement.html – jihor Feb 01 '18 at 16:54

1 Answers1

1

You must fully qualify QName including its package name in the constructor like:

var qName = new javax.xml.namespace.QName("xxx", "MyClass");
Serg M Ten
  • 5,568
  • 4
  • 25
  • 48
  • 1
    Ok. It seems that the JS VM has a native type called QName the same name as the one imported from `javax.xml.namespace` package. When I fully qualify the QName type it works. I can't believe it first time :). Thanks a lot. – TrapII Feb 07 '18 at 09:20