Наследование Jaxb с использованием замещения, но не для корневого элемента
Я шел через БлейзБлог сhttp://blog.bdoughan.com/2010/11/jaxb-and-inheritance-using-substitution.html для наследования Jaxb с использованием замещения.
Я хочу реализовать то же самое, но не для корневого элемента. Я смотрю этот тип XML в качестве вывода.
1 A Street
2 B Street
xxx-xxx-xxxx
Ниже приведен файл 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;
@XmlElementRef
public List getContactInfo() {
return contactInfo;
}
public void setContactInfo(List 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 contacts = new ArrayList();
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);
}
}
В настоящее время я получаю следующее исключение
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.
Кто-нибудь может мне помочь в этом?
Спасибо, кватра