2012-12-26 92 views
4

我有一個這樣的SOAP請求:如何使用Java JAX-RPC添加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: 

我真的被困在這裏。好的,有人幫我在這裏。

回答

3

我做了很多研究,發現了我的錯誤。我沒有通過安全名稱空間URL。因此,而不是:

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

我把它作爲:

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

瞧它開始工作,也沒有命名空間的錯誤。希望這可以幫助遇到類似問題的其他人。