You definitely can not add classes dynamically to a JAXBContext. This has to do with maintaining the thread safety of JAXBContext.
I have recently posted an example on my blog explaining how to leverage the @XmlAnyElement annotation to generate a generic message that could have different payloads:
Root Object
The root element for the body property will be controlled by that objects @XmlRootElement.
package message;
import javax.xml.bind.annotation.*;
@XmlRootElement@XmlAccessorType(XmlAccessType.FIELD)
public class Message {
@XmlAttribute
private String to;
@XmlAttribute
private String from;
@XmlAnyElement
private Object body;
}
Creating the JAXBContext
Instead of creating the JAXBContext on an array of classes, the JAXBContext could be created on a context path:
JAXBContext.newInstance("message:customer:product");
This context path includes 3 package names seperated by the colon ':' character. In each of these packages we need to include a file named jaxb.index with a list of files. Below is an example of the jaxb.index file in the customer package:
Address
Customer
When we want to add a model to represent orders to our framework we would extend our JAXBContext creation to like (this String could be passed in as a variable):
JAXBContext.newInstance("message:customer:product:order");