You cannot marshal only String , since it has no root element information (hence the exception to the missing @XmlRootElement annotation), but you can wrap it in a JAXBElement instance and then marshal that. JAXBElement is another way to pass this root element information to JAXB.
JAXBElement creation JAXBElement
JAXBElement<String> jaxbElement = new JAXBElement(new QName("root-element"), String.class, string);
If you created your model from an XML schema
If you created your object model from an XML schema. And you have a top-level XML element, which is a data type, for example xs:string , then a convenience method will be created for the generated ObjectFactory class, which will help you create an instance of JAXBElement .
Blaise donough
source share