我試圖編組一個具有JAXB(2.1.12)的DataHandler
字段的對象。對於流式支持,DataHandler
使用@XmlAttachmentRef
進行註釋。通過Web服務進行序列化和流式傳輸(Metro on JBoss)可以正常工作,但使用JAXB進行簡單編組卻不行。如何編組一個DataHandler用JAXB註解爲@XmlAttachmentRef?
這裏是一個精簡的例子:
public class DataHandlerAttachmentSerialization {
@XmlRootElement
static class RootObject {
@XmlElement
@XmlAttachmentRef // Works without this, but required for streaming
DataHandler dataHandler = new DataHandler(
new com.sun.xml.ws.util.ByteArrayDataSource(
" ".getBytes(), "application/octet-stream"));
}
@Test
public void test() throws JAXBException {
JAXBContext context = JAXBContext.newInstance(RootObject.class);
Marshaller marshaller = context.createMarshaller();
StringWriter writer = new StringWriter();
marshaller.marshal(new RootObject(), writer);
Assert.assertNotNull(writer.toString());
}
}
運行這個測試得出以下堆棧跟蹤:
javax.xml.bind.MarshalException
- with linked exception:
[com.sun.xml.bind.api.AccessorException: java.lang.NullPointerException]
at com.sun.xml.bind.v2.runtime.MarshallerImpl.write(MarshallerImpl.java:318)
at com.sun.xml.bind.v2.runtime.MarshallerImpl.marshal(MarshallerImpl.java:244)
at javax.xml.bind.helpers.AbstractMarshallerImpl.marshal(AbstractMarshallerImpl.java:96)
...
Caused by: com.sun.xml.bind.api.AccessorException: java.lang.NullPointerException
at com.sun.xml.bind.v2.runtime.XMLSerializer.reportError(XMLSerializer.java:246)
at com.sun.xml.bind.v2.runtime.XMLSerializer.reportError(XMLSerializer.java:261)
at com.sun.xml.bind.v2.runtime.ClassBeanInfoImpl.serializeBody(ClassBeanInfoImpl.java:335)
at com.sun.xml.bind.v2.runtime.XMLSerializer.childAsSoleContent(XMLSerializer.java:593)
at com.sun.xml.bind.v2.runtime.ClassBeanInfoImpl.serializeRoot(ClassBeanInfoImpl.java:320)
at com.sun.xml.bind.v2.runtime.XMLSerializer.childAsRoot(XMLSerializer.java:494)
at com.sun.xml.bind.v2.runtime.MarshallerImpl.write(MarshallerImpl.java:315)
... 25 more
Caused by: com.sun.xml.bind.api.AccessorException: java.lang.NullPointerException
at com.sun.xml.bind.v2.runtime.reflect.AdaptedAccessor.get(AdaptedAccessor.java:74)
at com.sun.xml.bind.v2.runtime.reflect.TransducedAccessor$CompositeTransducedAccessorImpl.writeLeafElement(TransducedAccessor.java:250)
at com.sun.xml.bind.v2.runtime.property.SingleElementLeafProperty.serializeBody(SingleElementLeafProperty.java:98)
at com.sun.xml.bind.v2.runtime.ClassBeanInfoImpl.serializeBody(ClassBeanInfoImpl.java:332)
... 29 more
Caused by: java.lang.NullPointerException
at com.sun.xml.bind.v2.runtime.SwaRefAdapter.marshal(SwaRefAdapter.java:80)
at com.sun.xml.bind.v2.runtime.SwaRefAdapter.marshal(SwaRefAdapter.java:65)
at com.sun.xml.bind.v2.runtime.reflect.AdaptedAccessor.get(AdaptedAccessor.java:72)
... 32 more
線80 SwaRefAdapter
是return am.addSwaRefAttachment(data);
和am
與AttachmentMarshaller am = XMLSerializer.getInstance().attachmentMarshaller;
分配如此看來attachmentMarshaller
如果@XmlAttachmentRef
存在,則此時爲空。
我需要設置不同的東西,還是以不同的方式使用JAXB API來處理附件?