使用替换而不是根元素的Jaxb继承 [英] Jaxb Inheritance using Substitution but not to root Element

查看:153
本文介绍了使用替换而不是根元素的Jaxb继承的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在浏览Blaise的博客 http:/ /blog.bdoughan.com/2010/11/jaxb-and-inheritance-using-substitution.html 使用替换进行Jaxb继承。

I was going through the Blaise's Blog http://blog.bdoughan.com/2010/11/jaxb-and-inheritance-using-substitution.html for Jaxb Inheritance using Substitution.

我想要实现相同但不是根元素。我正在寻找这种类型的XML作为输出。

I want to implement the same but not to the root element. I am looking this type of XML as a output.

   <?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>

以下是Configuration.java

Following is the 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

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

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

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

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);
    }
 }

目前我收到以下例外

    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.

有人可以帮我解决这个问题吗?

Could anybody help me out on this?

谢谢,
Kwatra

Thanks, Kwatra

推荐答案

问题#1 - 子类

JAXB(JSR-222) ) 实现无法自动发现子类。您可以通过在 ContactInfo 类上使用 @XmlSeeAlso 注释来引用子类来解决第一个异常:

A JAXB (JSR-222) implementation can not auto discover subclasses. You can solve the first exception by using an @XmlSeeAlso annotation on the ContactInfo class to reference the subclasses:

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

或者您可以在创建 JAXBContext 时引用它们。

Or you can reference them when you create the JAXBContext.

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






问题#2 - 映射

使用 @XmlElementRef 时,需要将其与 @XmlRootElement <配对/ code>。如果你不想走这条路,你可以改用 @XmlElements

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



查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆