Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
768 views
in Technique[技术] by (71.8m points)

java - JAXB xsi:type subclass unmarshalling not working

I'm following the instructions on using xsi:type from this oft-cited blog post:

http://blog.bdoughan.com/2010/11/jaxb-and-inheritance-using-xsitype.html

Basically I have this:

public abstract class ContactInfo {
}

public class Address extends ContactInfo {

    private String street;

    public String getStreet() {
        return street;
    }

    public void setStreet(String street) {
        this.street = street;
    }
}

@XmlRootElement
public class Customer {

    private ContactInfo contactInfo;

    public ContactInfo getContactInfo() {
        return contactInfo;
    }

    public void setContactInfo(ContactInfo contactInfo) {
        this.contactInfo = contactInfo;
    }
}

And this test:

@Test
public void contactTestCase() throws JAXBException, ParserConfigurationException, IOException, SAXException {
    Customer customer = new Customer();
    Address address = new Address();
    address.setStreet("1 A Street");
    customer.setContactInfo(address);

    JAXBContext jc = JAXBContext.newInstance(Customer.class, Address.class, PhoneNumber.class);
    StringWriter writer = new StringWriter();
    Marshaller marshaller = jc.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    marshaller.marshal(customer, writer);
    String s = writer.toString();
    System.out.append(s);

    StringInputStream sis = new StringInputStream(s);
    DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = documentBuilderFactory.newDocumentBuilder();
    Document doc = db.parse(sis);

    Unmarshaller um = jc.createUnmarshaller();
    JAXBElement result = um.unmarshal(doc, Customer.class);
    Customer f = (Customer) result.getValue();

    writer = new StringWriter();
    marshaller.marshal(customer, writer);
    s = writer.toString();
    System.out.append(s);
}

And I get this result:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<customer>
    <contactInfo xsi:type="address" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
        <street>1 A Street</street>
    </contactInfo>
</customer>

javax.xml.bind.UnmarshalException: Unable to create an instance of blog.inheritance.ContactInfo

I've tried the default implementation of JAXB, jaxb-impl-2.1.2 and based off this bug, I've tried jaxb-impl-2.2.6-b38.jar. None of it works.

Is this not supposed to work or am I missing some setup?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

In your test case you need to specify that the DocumentBuilderFactory is namespace aware. Without this setting the DOM input to your JAXB implemenation won't contain a properly formed xsi:type attribute.

    documentBuilderFactory.setNamespaceAware(true);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...