我正在尝试使用JAXB(2.1.12)编组一个具有DataHandler
字段的对象。对于流媒体支持,DataHandler
使用@ XmlAttachmentRef
进行注释。通过Web服务进行序列化和流式处理(JBoss上的Metro)工作正常,但是使用JAXB进行简单的编组却没有。
这是一个精简的例子:
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
SwaRefAdapter
中的第80行return am.addSwaRefAttachment(data);
,am
被AttachmentMarshaller am = XMLSerializer.getInstance().attachmentMarshaller;
分配,因此如果attachmentMarshaller
@XmlAttachmentRef
为空,则此时{{1}}为空存在。
我是否需要以不同的方式设置内容,或以不同的方式使用JAXB API来处理附件?
答案 0 :(得分:5)
在JAX-WS环境之外使用JAXB时,JAXB实现应默认将XML文档中的二进制内容内联为base64Binary。
这似乎是JAXB(包含在JDK / JRE中的RI)的Metro实现中的错误。如果您使用其他JAXB实现,例如EclipseLink JAXB (MOXy),这将正常工作。要使用EclipseLink MOXy,请将eclipselink.jar(available here)添加到类路径中,并在以下内容中添加jaxb.properties文件和模型类:
javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory
JAX-WS注册AttachmentMarshallers& AttachmentUnmarshallers处理JAXB模型中的二进制内容。如果需要,可以在使用JAXB独立时指定自己的。