Posted by Unknown on 7:55 AM
Labels:

If I add the @XmlRootElement annotation it works, but auto generated class not...why?




XJC does try to put @XmlRootElement
annotation on a class that we generate from a complex type. if we
can statically guarantee that a complex type won't be used by
multiple different tag names, we put @XmlRootElement annotation.

If:




<schema>
<element name="Abc">
<complexType> ...</complexType>
</element>
</schema>




then




@XmlRootElement

class Abc {

...

}




But if the schema is as follows,
then XJC can't eliminate the possibility that your type might be
used in other schemas (that can be compiled separately and put
together at runtime):
<schema>
<element name="Abc" type="Xyz" />
<complexType name="Xyz" />
</schema>

we will get,



class Bar {
}

class ObjectFactory {
JAXBElement createFoo(Xyz value) { ... }
}




Now, the crucial part in the above
inference done by XJC is that "your
schema might be used by other schemas that XJC isn't compiling right
now"
. This is certainly true for some users (and we learned that
the hard way), but it's also true that for many users this is too
conservative an assumption. It's often the case that what you are
compiling is indeed the whole thing, and you want XJC to know that
so that it can optimize the generated code more aggressively.



Work around:

If all you want is just to marshal an object without @XmlRootElement,
then you can just do:
marshaller.marshal( new JAXBElement(QName("uri","local"),

MessageType.class, messageType ));