gpt4 book ai didi

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

转载 作者:数据小太阳 更新时间:2023-10-29 02:27:05 25 4
gpt4 key购买 nike

你好,我有一个 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");

生成以下内容:

<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>

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

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

它说:

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)

我怎样才能做到这一点?

最佳答案

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

QName(String namespaceURI, String localPart)

QName constructor specifying the Namespace URI and local part.

关于java - HeaderElements 必须是命名空间限定的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13612504/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com