如何在SOAP HEader中将子节点添加到childNode?

时间:2012-02-23 19:23:04

标签: java soap soapheader

我正在尝试在里面创建子节点,它应该如下所示

   <soapenv:Header>
       <wsse:Security soapenv:mustUnderstand="1" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
         <wsse:UsernameToken wsu:Id="UsernameToken-21" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
           <wsse:Username>USER</wsse:Username>
           <wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">PAASS</wsse:Password>
           <wsse:Nonce EncodingType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary">==</wsse:Nonce>
           <wsu:Created>2012-02-22T16:27:29.088Z</wsu:Created>
         </wsse:UsernameToken>
       </wsse:Security>
     </soapenv:Header>

以下

    SOAPEnvelope envelope = soapPart.getEnvelope();
    SOAPElement userNameToken = envelope.getHeader().addChildElement("Usernametoken", WSSE_NS_PREFIX, WSSE_NS);
    SOAPElement user = userNameToken.addChildElement("UserName", WSSE_NS, WSSE_NS_PREFIX);

我在尝试这样做时收到以下错误:

    NAMESPACE_ERR: An attempt is made to create or change an object in a way which is incorrect with regard to namespaces.

1 个答案:

答案 0 :(得分:1)

似乎你混合了参数的顺序(WSSE_NS vs WSSE_NS_PREFIX) 它应该是

SOAPEnvelope envelope = soapPart.getEnvelope();
SOAPElement userNameToken = envelope.getHeader().addChildElement("Usernametoken", WSSE_NS_PREFIX, WSSE_NS);
SOAPElement user = userNameToken.addChildElement("UserName", WSSE_NS_PREFIX, WSSE_NS);