java—如何用jaxb封送注解为@xmlattachmentref的数据处理程序?

xxslljrj  于 2021-07-05  发布在  Java
关注(0)|答案(1)|浏览(422)

我正试图封送一个有 DataHandler jaxb(2.1.12)的字段。对于流媒体支持 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

第80行输入 SwaRefAdapterreturn am.addSwaRefAttachment(data); 以及 am 被分配了 AttachmentMarshaller am = XMLSerializer.getInstance().attachmentMarshaller; 看来是这样 attachmentMarshaller 如果 @XmlAttachmentRef 存在。
我是否需要进行不同的设置,或者以不同的方式使用jaxbapi来处理附件?

n3ipq98p

n3ipq98p1#

当在jax-ws环境之外使用jaxb时,jaxb实现应该是默认的内联,将xml文档中的二进制内容作为base64binary。
这似乎是jaxb的metro实现中的一个bug(jdk/jre中包含的ri)。如果您使用另一个jaxb实现,如eclipselinkjaxb(moxy),这将正常工作。要使用eclipselink moxy,请将eclipselink.jar(此处提供)添加到类路径中,并在模型类中添加jaxb.properties文件,其中包含以下内容:

javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory

jax-ws注册attachmentmarshaller和attachmentunmarshaller来处理jaxb模型中的二进制内容。如果需要,可以在使用jaxb standalone时指定自己的。
http://download.oracle.com/docs/cd/e17409_01/javase/6/docs/api/javax/xml/bind/attachment/attachmentmarshaller.html

相关问题