Unable to marshal type as XML element because @XmlRootElement annotation is missing Unable to marshal type as XML element because @XmlRootElement annotation is missing xml xml

Unable to marshal type as XML element because @XmlRootElement annotation is missing


public String toXML(T object) throws JAXBException {  StringWriter stringWriter = new StringWriter();  JAXBContext jaxbContext = JAXBContext.newInstance(T.class);  Marshaller jaxbMarshaller = jaxbContext.createMarshaller();  // format the XML output  jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);  QName qName = new QName("com.yourModel.t", "object");  JAXBElement<T> root = new JAXBElement<Bbb>(qName, T.class, object);  jaxbMarshaller.marshal(root, stringWriter);  String result = stringWriter.toString();  LOGGER.info(result);  return result;}

Here is the article I use when I have to marshal/unmarshal without @XmlRootElement: http://www.source4code.info/2013/07/jaxb-marshal-unmarshal-with-missing.html

All the best hope it helps :)


Note that the error message talks about FreightOfferDetail, not FreightOffer.

Based on that, I would expect that somewhere (outside the provided code), you're asking a Detail to be marshalled.

If you want to be able to do that (with JAXB?) you need to annotate that class as well with the XmlRootElement annotation.


You can use the ObjectFactory class to workaround for the classes which doesn't have the @XmlRootElement. ObjectFactory has overloaded methods.

Method:1 It does simple creation of the object and

Method:2 It will wrap the object with @JAXBElement.

Always to use Method:2 to avoid javax.xml.bind.MarshalException - with linked exception missing an @XmlRootElement annotation

Method:1

public GetCountry createGetCountry() {        return new GetCountry();    }

Method:2

 @XmlElementDecl(namespace = "my/name/space", name = "getCountry") public JAXBElement<GetCountry> createGetCountry(GetCountry value) {        return new JAXBElement<GetCountry>(_GetCountry_QNAME, GetCountry.class, null, value);    }

for more information follow this stackoverflow-question

Hope this will be helpful...