I wrote a bean (BaseBeanEx) extending a JAXB annotated bean (BaseBean). The BaseBean is in a List somewhere in the datastructure and can’t be changed. The Software does an explicit cast to BaseBeanEx whenever it is needed. I also wrote an ObjectFactory to create BaseBeanEx instead of BaseBean. This all works fine, but now I added a afterUnmarshal method to BaseBeanEx which never gets called.
Is this a bug or is this according to the specs? If later is the case, is there some elegant work around?
I’m using the default JAXB engine.
Note: I’m the EclipseLink JAXB (MOXy) lead and a member of the JAXB 2 (JSR-222) expert group.
The reason that
afterUnmarshalis not being called onBaseBeanExis that the metadata was built on theBaseBeanclass. To get your use case to work you need to let your JAXB impl know that you really want to map to instances ofBaseBeanEx.OPTION #1 – Any JAXB Implementation using Annotations
Root
You can use the
@XmlElementannotation to override the type of a field/property. In the example below the signature of the method isList<BaseBean>, but the@XmlElementannotation informs the JAXB implementation the property should be interpreted asList<BaseBeanEx>.OPTION #2 – Using MOXy’s External Mapping Document
If you can’t modify your domain model and are using MOXy as your JAXB provider then you can leverage its external mapping document to apply metadata without modifying your domain model.
bindings.xml
Demo
Below is some code that demonstrates how to bootstrap a
JAXBContextthat leverages the external mapping document. There is currently a bug where classes only referenced through the external mapping document won’t have there event methods registered (http://bugs.eclipse.org/376876). You can work around this issue by explicitly including this class in the list of classes used to create theJAXBContext.BaseBean
BaseBeanEx
Output
Below is the output that was generated by running the demo code.
For More Information