Jaxb inheritance using substitution but not root element

I went through the Blaise blog http://blog.bdoughan.com/2010/11/jaxb-and-inheritance-using-substitution.html to inherit Jaxb via replacement.

I want to implement the same thing, but not with the root element. I view this type of XML as a result.

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <configuration> <customer> <address> <street>1 A Street</street> </address> <address> <street>2 B Street</street> </address> <phoneNumber> <mobileNo>xxx-xxx-xxxx</mobileNo> </phoneNumber> </customer> </configuration> 

The following is a description of Configuration.java

  import javax.xml.bind.annotation.XmlRootElement; @XmlRootElement public class Configuration { private Customer customer; public Customer getCustomer() { return customer; } public void setCustomer(Customer customer) { this.customer = customer; } @Override public String toString() { return "\n Customer[ customer="+customer+"]"; } } 

Customer.java

  public class Customer { private List<ContactInfo> contactInfo; @XmlElementRef public List<ContactInfo> getContactInfo() { return contactInfo; } public void setContactInfo(List<ContactInfo> contactInfo) { this.contactInfo = contactInfo; } } 

Address.java

  public class Address extends ContactInfo { private String street; public String getStreet() { return street; } public void setStreet(String street) { this.street = street; } } 

PhoneNumber.java

  public class PhoneNumber extends ContactInfo{ private String mobileNo; public String getMobileNo() { return mobileNo; } public void setMobileNo(String mobileNo) { this.mobileNo = mobileNo; } } 

Demo.java

  import java.util.ArrayList; import java.util.List; import javax.xml.bind.JAXBContext; import javax.xml.bind.Marshaller; public class Demo { public static void main(String[] args) throws Exception { Configuration configuration = new Configuration(); Customer customer = new Customer(); List<ContactInfo> contacts = new ArrayList<ContactInfo>(); Address address = new Address(); address.setStreet("1 A Street"); contacts.add(address); Address address1 = new Address(); address1.setStreet("2 B Street"); contacts.add(address1); PhoneNumber phone = new PhoneNumber(); phone.setMobileNo("408 431 8829"); contacts.add(phone); customer.setContactInfo(contacts); configuration.setCustomer(customer); JAXBContext jc = JAXBContext.newInstance(Configuration.class); Marshaller marshaller = jc.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); marshaller.marshal(configuration, System.out); } } 

I am currently getting the following exception

  Exception in thread "main" com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException: 1 counts of IllegalAnnotationExceptions Invalid @XmlElementRef : Type "class Address" or any of its subclasses are not known to this context. 

Can someone help me with this?

Thanks Kwatra

+7
source share
1 answer

Problem # 1 - Subclasses

A JAXB (JSR-222) cannot automatically detect subclasses. You can resolve the first exception by using the @XmlSeeAlso annotation in the ContactInfo class to refer to subclasses:

 @XmlSeeAlso({Address.class, PhoneNumber.class}) public class ContactInfo { } 

Or you can reference them when creating a JAXBContext .

  JAXBContext jc = JAXBContext.newInstance(Configuration.class, Address.class, PhoneNumber.class); 

Problem # 2 - Display

When using @XmlElementRef you need to connect it with @XmlRootElement . If you do not want to go this route, you can use @XmlElements instead.

 @XmlElements({ @XmlElement(name="address", type=Address.class), @XmlElement(name="phoneNumber", type=PhoneNumber.class) }) public List<ContactInfo> getContactInfo() { return contactInfo; } 
+12
source

All Articles