Is it possible to marshall all Java POJOs to XML without using JAXB annotations without configuring every POJO class independently?
PS: the context is a Rest Resource with Jersey 2.
JAXB (JSR-222) implementations including MOXy do not require any annotations. In the absence of an @XmlRootElement
annotation you need to wrap your object in an instance of JAXBElement
.
JAXBContext jc = JAXBContext.newInstance(Foo.class);
Foo foo = new Foo();
JAXBElement<Foo> je = new JAXBElement(new QName("root-element"), Foo.class, foo);
Marshaller marshaller = jc.createMarshaller();
marshaller.marshal(je, System.out);
For More Information
You can refer to the following article from my blog for a full example: