Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Marshalling fails with exception about MarshallerImpl not known to this context

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

This works

The utility class can successfully unmarshal a valid XML document into an instance of the ‘root’ class WordMergeInfo. For example this works fine:

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

This doesn’t work

But marshalling to a string fails. 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 with the following error:

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 a JAXB class needed for marshalling cannot be found. So why can’t one of the JAXB implementation classes be found, when the same class is in the stack trace?

Context

This error showed up in a unit test of my class, run under Maven. The dependencies are:

  • javax.xml.bind:jaxb-api:2.1
  • com.sun.xml.bind:jaxb-impl:2.1.13

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

The Maven test class path is:

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 insight is appreciated.

like image 586
pharsicle Avatar asked Feb 22 '12 03:02

pharsicle


People also ask

What is marshalling error?

A MarshalException is thrown if a java. io. IOException occurs while marshalling the remote call header, arguments or return value for a remote method call. A MarshalException is also thrown if the receiver does not support the protocol version of the sender.

What is JAXB exception?

This exception indicates that an error has occurred while performing a validate operation. Methods in javax.xml.bind that throw JAXBException. Modifier and Type. Method and Description. abstract Marshaller.

What is javax xml bind MarshalException?

A javax.xml.bind.MarshalException error may occur when a null object is used as an argument to a java.util.List parameter on a JAX-WS web method.

What is JAXB context?

The JAXBContext class provides the client's entry point to the JAXB API. It provides an abstraction for managing the XML/Java binding information necessary to implement the JAXB binding framework operations: unmarshal, marshal and validate.


1 Answers

nor any of its super class is known to this context

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

Your error is on this line:

m.marshal(m, writer);

You are trying to marshal the marshaller itself. You probably meant to marshal a WordMergeInfo object.

like image 158
casablanca Avatar answered Sep 22 '22 22:09

casablanca