我目前正在为应由ONVIF兼容客户端(例如视频管理系统)发现的设备开发WS-Discovery规范。为了封送Java对象并将它们包装到SOAP信封中,我正在使用this example中描述的方法。如您所见,在此示例中,使用JAXB marshaller实现将Java对象编组到文档中,将其添加到SOAPMessage对象的主体部分,然后将SOAPMessage写入ByteArrayOutputStream。

通过这种方法,我目前正在以以下形式生成消息(例如ProbeMatches对Probe请求的响应):

<?xml version="1.0" encoding="UTF-8"?>
<env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope" xmlns:wsa="http://www.w3.org/2005/08/addressing">
    <env:Header>
        <wsa:Action env:mustUnderstand="true">http://docs.oasis-open.org/ws-dd/ns/discovery/2009/01/ProbeMatches</wsa:Action>
        <wsa:MessageID>urn:uuid:9a7e8bc4-d4aa-4269-bbd6-2414084f47fe</wsa:MessageID>
        <wsa:To env:mustUnderstand="true">http://www.w3.org/2005/08/addressing/anonymous</wsa:To>
       <wsa:RelatesTo>urn:uuid:f3e33900-6284-11e8-80b1-add204fec0a3</wsa:RelatesTo>
    </env:Header>
    <env:Body>
        <ns2:ProbeMatches xmlns:ns2="http://docs.oasis-open.org/ws-dd/ns/discovery/2009/01" xmlns="http://www.w3.org/2005/08/addressing">
             <ns2:ProbeMatch>
                 <EndpointReference>
                     <Address>urn:uuid:09edc0f4-d65c-3545-aba5-a59f86348521</Address>
                     <ReferenceParameters />
                 </EndpointReference>
                 <ns2:Types xmlns:dn="http://www.onvif.org/ver10/network/wsdl">dn:NetworkVideoTransmitter</ns2:Types>
                 <ns2:Scopes>onvif://www.onvif.org/name/MyDevice onvif://www.onvif.org/hardware/MyHardware onvif://www.onvif.org/location onvif://www.onvif.org/Profile/Streaming</ns2:Scopes>
                 <ns2:XAddrs>http://192.168.0.10:8080/onvif/device_service</ns2:XAddrs>
                 <ns2:MetadataVersion>1</ns2:MetadataVersion>
             </ns2:ProbeMatch>
        </ns2:ProbeMatches>
    </env:Body>
</env:Envelope>


如您所见,WS-Addressing的名称空间声明存在两次,一次在信封中,一次在ProbeMatches标记中。另外,我想将名称空间声明从ProbeMatches标记移到信封。
所以,我真正想要实现的是:

<?xml version="1.0" encoding="UTF-8"?>
<env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope" xmlns:wsa="http://www.w3.org/2005/08/addressing" xmlns:wsd="http://docs.oasis-open.org/ws-dd/ns/discovery/2009/01">
    <env:Header>
        <wsa:Action env:mustUnderstand="true">http://docs.oasis-open.org/ws-dd/ns/discovery/2009/01/ProbeMatches</wsa:Action>
        <wsa:MessageID>urn:uuid:9a7e8bc4-d4aa-4269-bbd6-2414084f47fe</wsa:MessageID>
        <wsa:To env:mustUnderstand="true">http://www.w3.org/2005/08/addressing/anonymous</wsa:To>
       <wsa:RelatesTo>urn:uuid:f3e33900-6284-11e8-80b1-add204fec0a3</wsa:RelatesTo>
    </env:Header>
    <env:Body>
        <wsd:ProbeMatches>
             <wsd:ProbeMatch>
                 <wsa:EndpointReference>
                     <wsa:Address>urn:uuid:09edc0f4-d65c-3545-aba5-a59f86348521</wsa:Address>
                     <wsa:ReferenceParameters />
                 </wsa:EndpointReference>
                 <wsd:Types xmlns:dn="http://www.onvif.org/ver10/network/wsdl">dn:NetworkVideoTransmitter</wsd:Types>
                 <wsd:Scopes>onvif://www.onvif.org/name/MyDevice onvif://www.onvif.org/hardware/MyHardware onvif://www.onvif.org/location onvif://www.onvif.org/Profile/Streaming</wsd:Scopes>
                 <wsd:XAddrs>http://192.168.0.10:8080/onvif/device_service</wsd:XAddrs>
                 <wsd:MetadataVersion>1</wsd:MetadataVersion>
             </wsd:ProbeMatch>
        </wsd:ProbeMatches>
    </env:Body>
</env:Envelope>


如何使用JAXB以及使用SOAPMessage提到的示例来实现这一目标?问题在于,SOAPMassege似乎无法识别已封送的正文部分中已经存在的名称空间声明。我也欢迎采用完全不同的编组方法。

作为附加信息,这是被封送的ProbeMatchesType和ProbeMatchType类的结构(我将@XmlRootElement批注手动添加到ProbeMatchesType,其余部分是从官方ONVIF WSDL文件生成的):

@XmlRootElement(name = "ProbeMatches")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "ProbeMatchesType", propOrder = {
    "probeMatch",
    "any"
})
public class ProbeMatchesType {

    @XmlElement(name = "ProbeMatch")
    protected List<ProbeMatchType> probeMatch;
    @XmlAnyElement(lax = true)
    protected List<Object> any;
    @XmlAnyAttribute
    private Map<QName, String> otherAttributes = new HashMap<QName, String>();

    ...
}

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "ProbeMatchType", propOrder = {
    "endpointReference",
    "types",
    "scopes",
    "xAddrs",
    "metadataVersion",
    "any"
})
public class ProbeMatchType {

    @XmlElement(name = "EndpointReference", namespace = "http://www.w3.org/2005/08/addressing", required = true)
    protected W3CEndpointReference endpointReference;
    @XmlList
    @XmlElement(name = "Types")
    protected List<QName> types;
    @XmlElement(name = "Scopes")
    protected ScopesType scopes;
    @XmlList
    @XmlElement(name = "XAddrs")
    protected List<String> xAddrs;
    @XmlElement(name = "MetadataVersion")
    @XmlSchemaType(name = "unsignedInt")
    protected long metadataVersion;
    @XmlAnyElement(lax = true)
    protected List<Object> any;
    @XmlAnyAttribute
    private Map<QName, String> otherAttributes = new HashMap<QName, String>();

    ...
}


在此先感谢您的帮助!

更新资料

在我当前的方法中,当我从ProbeMatchesType类删除@XmlRootElement时,出现以下异常:

com.sun.istack.internal.SAXException2: unable to marshal type "org.xmlsoap.schemas.ws._2005._04.discovery.ProbeMatchesType" as an element because it is missing an @XmlRootElement annotation
    at com.sun.xml.internal.bind.v2.runtime.XMLSerializer.reportError(XMLSerializer.java:234)
    at com.sun.xml.internal.bind.v2.runtime.ClassBeanInfoImpl.serializeRoot(ClassBeanInfoImpl.java:323)
    at com.sun.xml.internal.bind.v2.runtime.XMLSerializer.childAsRoot(XMLSerializer.java:479)
    at com.sun.xml.internal.bind.v2.runtime.MarshallerImpl.write(MarshallerImpl.java:308)


发生这种情况是因为当前ProbeMatchesType对象实际上实际上是XML封送处理的根对象。封送文档的对象稍后添加到SOAPMessage对象的主体部分,该对象最终被写入ByteArrayOutputStream时,将所有内容包装到SOAP信封中(就像您在开始时已经链接的example中看到的那样)我的问题)。

最佳答案

尝试从ProbeMatchesType类中删除@XmlRootElement(name =“ ProbeMatches”)。

@XmlRootElement应该用于顶级类,在这种情况下,顶级类是Envelope。

10-06 12:00