My application receives, from another application, xml of the form:
<targetedMessage>
<sender>the sender</sender>
<payload class="other.app.classpath.Foo">
<id>1</id>
</payload>
</targetedMessage>
where Foo
is any one of several classes which exist in both my module and the other application, and implements a common interface:
@XmlAccessorType(value = XmlAccessType.FIELD)
@XmlRootElement
public class Foo implements MyInterface {
private long id;
\\ getters and setters
}
and the TargetedMessage
class is:
@XmlAccessorType(value = XmlAccessType.FIELD)
@XmlRootElement
public class TargetedMessage {
private String sender;
private MyInterface payload;
\\ getters and setters
}
I have an enum which maps the other apps class-paths to their classes in my module:
public enum MyClasses {
FOO(Foo.class, "other.app.classpath.Foo"),
BAR(Bar.class, "other.app.classpath.Bar"),
\\ ...
}
Using JAXB, is it possible to unmarshal the xml so that the payload is of the correct type (in the above case, the payload class would be Foo
).