XML > JAXB > Serialize interfaces using XmlSeeAlso
Serialize interfaces using XmlSeeAlso
The following example shows serializing a POJO having reference to an interface.
We need to annotate the interface with
@XmlJavaTypeAdapter(AnyTypeAdapter.class) annotation
to specify that an implementation of interface has to be serialized to XML.
JAXB also requires the list of all possible implementations which needs to
be serialized. We can specify the same using
@XmlSeeAlso( { LicenseAlertDetails.class })
annotation at the root element level.
public static void main (String [] args) throws JAXBException {
Alert alert = new Alert();
alert.setName("ABC");
alert.setAlertType("License");
LicenseAlertDetails lic = new LicenseAlertDetails();
lic.setAlertDetailsId("123");
lic.setLicenseName("ABCD");
lic.setLicenseCount(6);
alert.setAlertDetails(lic);
/**
* Create JAXB Context from the classes to be serialized
*/
JAXBContext context = JAXBContext.newInstance(Alert.class);
Marshaller m = context.createMarshaller();
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
m.marshal(alert, System.out);