Marshalling cannot be excluded from MarshallerImpl not known to this context

I have a relatively simple package of 8 Java classes created from an XML schema using JAXB XJC. I also have a utility class to marshal and unwind instances of the class.

It works

A utility class can successfully decouple a valid XML document into an instance of the WordMergeInfo root class. For example, this works great:

  JAXBContext jc = JAXBContext.newInstance(WordMergeInfo.class); Unmarshaller um = jc.createUnmarshaller(); return (WordMergeInfo)um.unmarshal(inputStream); 

This does not work

But the string is not sorted. In this code:

  JAXBContext jc = JAXBContext.newInstance(WordMergeInfo.class); Marshaller m = jc.createMarshaller(); StringWriter writer = new StringWriter(); m.marshal(m, writer); return writer.toString(); 

the call to Marshaller.marshal fails:

 javax.xml.bind.JAXBException: class com.sun.xml.bind.v2.runtime.MarshallerImpl nor any of its super class is known to this context. at com.sun.xml.bind.v2.runtime.JAXBContextImpl.getBeanInfo(JAXBContextImpl.java:594) at com.sun.xml.bind.v2.runtime.XMLSerializer.childAsRoot(XMLSerializer.java:482) at com.sun.xml.bind.v2.runtime.MarshallerImpl.write(MarshallerImpl.java:315) at com.sun.xml.bind.v2.runtime.MarshallerImpl.marshal(MarshallerImpl.java:244) 

As I understand it, nor any of its super class is known to this context means that the JAXB class needed for sorting cannot be found. So why can't you find one of the JAXB implementation classes if the same class is in the stack trace?

Context

This error appeared in the unit test of my class, launched under Maven. Dependencies:

  • javax.xml.bind: JAXB-api: 2.1
  • com.sun.xml.bind: JAXB-implemented: 2.1.13

I got the same error with earlier versions of these (2.0 and 2.0.3, respectively).

Maven Test Class Path:

 C:\Users\mstra.CUSTMAN\Workspace\DARTCorrModule\xml\target\test-classes C:\Users\mstra.CUSTMAN\Workspace\DARTCorrModule\xml\target\classes C:\Users\mstra.CUSTMAN\.m2\repository\javax\xml\bind\jaxb-api\2.1\jaxb-api-2.1.jar C:\Users\mstra.CUSTMAN\.m2\repository\javax\xml\stream\stax-api\1.0-2\stax-api-1.0-2.jar C:\Users\mstra.CUSTMAN\.m2\repository\javax\activation\activation\1.1\activation-1.1.jar C:\Users\mstra.CUSTMAN\.m2\repository\com\sun\xml\bind\jaxb-impl\2.1.13\jaxb-impl-2.1.13.jar C:\Users\mstra.CUSTMAN\.m2\repository\junit\junit\4.8.2\junit-4.8.2.jar C:\Users\mstra.CUSTMAN\.m2\repository\org\mockito\mockito-all\1.8.5\mockito-all-1.8.5.jar C:\Users\mstra.CUSTMAN\.m2\repository\javax\ejb\ejb-api\3.0\ejb-api-3.0.jar C:\Users\mstra.CUSTMAN\.m2\repository\org\slf4j\slf4j-api\1.6.4\slf4j-api-1.6.4.jar 

Any understanding is understood.

+6
source share
1 answer

and not one of his superclass is known to this context

This means that the class is not registered as a marshallable class in the context of JAXB.

Your error on this line:

 m.marshal(m, writer); 

You are trying to marshal the marshaller himself. You probably planned to marshal the WordMergeInfo object.

+7
source

Source: https://habr.com/ru/post/909037/


All Articles