I'm using JAXB to marshal a class that has an instance variable that's declared as class Foo. At runtime that instance variable is set to an instance of a subclass of Foo, let's say FooBar. There are basic JAXB annotations on both class Foo and FooBar.
The XML output shows that an instance of Foo is being marshaled instead of FooBar. Is there something specific I have to do in terms of annotations to tell JAXB how to properly marshal the runtime subclass in lieu of the declared superclass?
I tried an experiment in which I directly marshaled an instance variable foo of type Foo that was set to an instance of FooBar at runtime. It correctly marshaled an instance of FooBar.
This is clearly different from marshaling a class that contains an instance variable of type Foo because the marshal call is made on the containing class and JAXB just marshals that classes fields (I'm using the @XmlAccessorType(XmlAccessType.FIELD) annotation).
My expectation was that JAXB could properly inspect the runtime type of the instance variable but perhaps that's not the case. Does this situation call for using an implementation of XmlAdapter as described here?
JAXB inheritance, unmarshal to subclass of marshaled class
Thanks,
Chris