tl  tr
  Home | Tutorials | Articles | Videos | Products | Tools | Search
Interviews | Open Source | Tag Cloud | Follow Us | Bookmark | Contact   
 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.

File Name  :  
com/bethecoder/tutorials/jaxb/common/interfaces/IAlertDetails.java 
   
package com.bethecoder.tutorials.jaxb.common.interfaces;

import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;

import com.sun.xml.bind.AnyTypeAdapter;

@XmlRootElement
@XmlJavaTypeAdapter(AnyTypeAdapter.class)
public interface IAlertDetails {
  public String getAlertDetailsId();
}
   

File Name  :  
com/bethecoder/tutorials/jaxb/common/interfaces/LicenseAlertDetails.java 
   
package com.bethecoder.tutorials.jaxb.common.interfaces;

public class LicenseAlertDetails implements IAlertDetails {
  private String alertDetailsId;
  private String licenseName;
  private int licenseCount;
  
  public int getLicenseCount() {
    return licenseCount;
  }
  public void setLicenseCount(int licenseCount) {
    this.licenseCount = licenseCount;
  }
  public String getAlertDetailsId() {
    return alertDetailsId;
  }
  public void setAlertDetailsId(String alertDetailsId) {
    this.alertDetailsId = alertDetailsId;
  }
  public String getLicenseName() {
    return licenseName;
  }
  public void setLicenseName(String licenseName) {
    this.licenseName = licenseName;
  }
}
   

File Name  :  
com/bethecoder/tutorials/jaxb/common/interfaces/Alert.java 
   
package com.bethecoder.tutorials.jaxb.common.interfaces;

import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlSeeAlso;

@XmlRootElement
@XmlSeeAlso( { LicenseAlertDetails.class })
public class Alert {
  private String name;
  private String alertType;
  private IAlertDetails alertDetails;
  
  public String getName() {
    return name;
  }
  public void setName(String name) {
    this.name = name;
  }
  public String getAlertType() {
    return alertType;
  }
  public void setAlertType(String alertType) {
    this.alertType = alertType;
  }
  public IAlertDetails getAlertDetails() {
    return alertDetails;
  }
  public void setAlertDetails(IAlertDetails alertDetails) {
    this.alertDetails = alertDetails;
  }
}
   

File Name  :  
com/bethecoder/tutorials/jaxb/tests/InterfaceTest.java 
Author  :  Sudhakar KV
Email  :  [email protected]
   
package com.bethecoder.tutorials.jaxb.tests;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;

import com.bethecoder.tutorials.jaxb.common.interfaces.Alert;
import com.bethecoder.tutorials.jaxb.common.interfaces.LicenseAlertDetails;

public class InterfaceTest {

  public static void main (String [] argsthrows 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);  

  }
}
   

It gives the following output,
File Name  :  OUTPUT



 
  


  
bl  br