如何使用Java JAX-RPC添加SOAP标头?

4

我有一个如下的SOAP请求:

    <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ch="urn://mfots.com/xmlmessaging/CH" xmlns:oas="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
<soapenv:Header>
<ch:MFprofileMnt>
<ch:myID>1458</ch:myID>
<ch:bigID>raptool</ch:bigID>
<ch:matID>5689</ch:matID>
</ch:MFprofileMnt>

现在我用Java创建了这样的请求:
        Name headerContextName = soapEnvelope.createName("MFprofileMnt", "ch","");
        SOAPHeaderElement soapHeaderElement = soapHeader.addHeaderElement(headerContextName);
        // mustUnderstand attribute is used to indicate
        // whether the header entry is mandatory or optional for the
        // recipient to process.
        soapHeaderElement.setMustUnderstand(true);
        //Now set the attribute children
        // create the first child element and set the value
        SOAPElement element1 = soapHeaderElement.addChildElement("myID", "ch");
        element1.setValue("1458");
        //create the second child element and set the value
        SOAPElement element2 = soapHeaderElement.addChildElement("bigID", "ch");
        element2.setValue("raptool");
        //create the third child element and set the value
        SOAPElement element3 = soapHeaderElement.addChildElement("matID", "ch");
        element3.setValue("5689");

然而,当我运行程序时,我一直收到这些错误信息:
org.w3c.dom.DOMException: NAMESPACE_ERR: An attempt is made to create or change an object in a way which is incorrect with regard to namespaces.
faultActor: null
faultDetail:

我真的陷入了困境。恳请有人在这里帮助我。

1个回答

3

我在这方面做了很多研究并发现了我的错误。我没有传递Security命名空间的URL。所以,与其使用:

Name headerContextName = soapEnvelope.createName("MFprofileMnt", "ch","");

我翻译为:

Name headerContextName = soapEnvelope.createName("MFprofileMnt", "ch",SOAP_Security_Namespace_URL);

终于,它开始工作了,没有遇到命名空间错误。希望这能帮助其他遇到类似问题的人。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接