čtvrtek 21. února 2013

JAXB marshal element without @XmlRootElement anotation

When you are trying marshal MyClass, that is not anotated as @XmlRootElement element, you can obtain:

unable to marshal type "org.company.MyClass" as an element
because it is missing an @XmlRootElement annotation

Possible workaround is marshal your object in this way:

Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
marshaller.marshal(new JAXBElement<MyClass>(new QName("NameSpaceUri","ElementName"), MyClass.class, myClassInstance), System.out);
view raw gistfile1.java hosted with ❤ by GitHub

I have found, that more details can be found here:
http://weblogs.java.net/blog/2006/03/03/why-does-jaxb-put-xmlrootelement-sometimes-not-always
http://stackoverflow.com/questions/819720/no-xmlrootelement-generated-by-jaxb

Žádné komentáře:

Okomentovat