java HeaderElements 必须是命名空间限定的

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13612504/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 13:24:33  来源:igfitidea点击:

HeaderElements must be namespace qualified

javaxmlsoap

提问by Ali Yucel Akgul

Hi there I have a java code to create a SOAP message to send a WSDL service.The code is as follows:

嗨,我有一个 java 代码来创建一个 SOAP 消息来发送一个 WSDL 服务。代码如下:

  SOAPConnectionFactory sfc = SOAPConnectionFactory.newInstance();
  SOAPConnection connection = sfc.createConnection();

  MessageFactory mf = MessageFactory.newInstance();
  SOAPMessage sm = mf.createMessage();

  SOAPHeader sh = sm.getSOAPHeader();
  SOAPBody sb = sm.getSOAPBody();


  QName bodyName = new QName("SendSMSInput");
  SOAPBodyElement bodyElement = sb.addBodyElement(bodyName);

  QName expiryDate = new QName("EXPIRY_DATE");
  SOAPElement node = bodyElement.addChildElement(expiryDate);
  node.addTextNode("TARIH GELECEK BURAYA");

  QName message_class = new QName("MESSAGE_CLASS");
  SOAPElement node2 = bodyElement.addChildElement(message_class);
  node2.addTextNode("MESSAGE_CLASS GELECEK BURAYA");

  QName s_date = new QName("S_DATE");
  SOAPElement node3 = bodyElement.addChildElement(s_date);
  node3.addTextNode("S_DATE GELECEK BURAYA");

  QName short_number = new QName("SHORT_NUMBER");
  SOAPElement node4 = bodyElement.addChildElement(short_number);
  node4.addTextNode("SHORT NUMBER GELECEK BURAYA");

  QName src_msisdn = new QName("SRC_MSISN");
  SOAPElement node5 = bodyElement.addChildElement(src_msisdn);
  node5.addTextNode("BO?LUK");

  QName to_rec = new QName("TO_RECEIVERS");
  SOAPElement node6 = bodyElement.addChildElement(to_rec);


  QName msisdn = new QName("msisdn");
  SOAPElement node6_1 = node6.addChildElement(msisdn);
  node6_1.addTextNode("BO?LUK");

  QName message_body = new QName("MESSAGE_BODY");
  SOAPElement node7 = bodyElement.addChildElement(message_body);


  QName message = new QName("message");
  SOAPElement node7_1 = node7.addChildElement(message);
  node7_1.addTextNode("BO?LUK");

which generates the following:

它生成以下内容:

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Header/>
<SOAP-ENV:Body>
    <SendSMSInput>
        <EXPIRY_DATE>TARIH GELECEK BURAYA</EXPIRY_DATE>
        <MESSAGE_CLASS>MESSAGE_CLASS GELECEK BURAYA</MESSAGE_CLASS>
        <S_DATE>S_DATE GELECEK BURAYA</S_DATE>
        <SHORT_NUMBER>SHORT NUMBER GELECEK BURAYA</SHORT_NUMBER>
        <SRC_MSISN>BO?LUK</SRC_MSISN>
        <TO_RECEIVERS>
            <msisdn>BO?LUK</msisdn>
        </TO_RECEIVERS>
        <MESSAGE_BODY>
            <message>BO?LUK</message>
        </MESSAGE_BODY>
    </SendSMSInput>
</SOAP-ENV:Body>

I need to add elements under the header. When I try

我需要在标题下添加元素。当我尝试

QName tokenHeader = new QName("token");
  SOAPHeaderElement tokenElement = sh.addHeaderElement(tokenHeader);

it says:

它说:

HeaderElements must be namespace qualified
Exception in thread "main" com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl: HeaderElements must be namespace qualified
    at com.sun.xml.internal.messaging.saaj.soap.impl.HeaderImpl.addHeaderElement(HeaderImpl.java:96)
    at soapgenerator.SOAPGenerator.main(SOAPGenerator.java:34)

How can I achieve this?

我怎样才能做到这一点?

回答by Has QUIT--Anony-Mousse

use the QNameconstructor that has a namespace part instead of the namespace-less constructor.

使用QName具有命名空间部分的构造函数而不是无命名空间的构造函数。

QName(String namespaceURI, String localPart)

QName constructor specifying the Namespace URI and local part.

QName(String namespaceURI, String localPart)

指定命名空间 URI 和本地部分的 QName 构造函数。