如何在SOAP请求中将Type属性设置为子元素?

时间:2016-09-27 17:35:00

标签: java soap

我需要将Type添加到密码子元素。

当前SOAP请求 -

<wsse:Security>
  <wsse:UsernameToken>
    <wsse:Username>user123</wsse:Username>
    <wsse:Password>xxxxx</wsse:Password>                        
  </wsse:UsernameToken>
</wsse:Security>

生成此代码 -

String prefix = "wsse";
SOAPElement security = header.addChildElement("Security", prefix);
SOAPElement token = security.addChildElement("UsernameToken", prefix);
token.addChildElement("Username", prefix).addTextNode("user123");
token.addChildElement("Password", prefix).addTextNode("xxxxx");

我需要生成像 -

这样的SOAP请求
<wsse:Security>
  <wsse:UsernameToken>
    <wsse:Username>user123</wsse:Username>
    <wsse:Password Type="http://abc.def-ghi.org/wss/2016/09/xyz-wss-username-token-profile-1.0#PasswordText">xxxxx</wsse:Password>                        
  </wsse:UsernameToken>
</wsse:Security>

1 个答案:

答案 0 :(得分:1)

String prefix = "wsse";
SOAPElement security = header.addChildElement("Security", prefix);
SOAPElement token = security.addChildElement("UsernameToken", prefix);
token.addChildElement("Username", prefix).addTextNode("user123");
SOAPElement passwordElement = token.addChildElement("Password",prefix).addTextNode('xxxxx');
passwordElement.setAttribute("Type", "http://abc.def-ghi.org/wss/2016/09/xyz-wss-username-token-profile-1.0#PasswordText");