发送 SOAP 消息
我正在尝试发送 SOAP 消息。
我使用下一个代码手动向消息添加标头:
public static void main(String[] args) {
try{
AmdocsServicesServiceagentLocator locator = new AmdocsServicesServiceagentLocator();
PortTypeEndpoint1BindingStub port = new PortTypeEndpoint1BindingStub(new URL("http://srvp7rd-tibco.rnd.local:8025/Process/SoapRequests/Amdocs-Services.serviceagent/PortTypeEndpoint1"),locator);
GetContactRelatedInfo parameters = new GetContactRelatedInfo();
GetContactRelatedInfoRequest request = new GetContactRelatedInfoRequest();
request.setPersonID("6610782925");
request.setPersonIDType("ID number (CPR)");
/* Creating an empty XML Document - We need a document*/
DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = dbfac.newDocumentBuilder();
Document doc = docBuilder.newDocument();
/* Creating the XML tree */
/* Create the root element and add it to the document */
Element root = doc.createElement("mul:MultiTenant");
doc.appendChild(root);
/* Adding the child to the root */
Element child = doc.createElement("mul:OpCo");
root.appendChild(child);
/* Add text element to the child */
Text text = doc.createTextNode("DENMARK");
child.appendChild(text);
/* Adding the child to the root */
child = doc.createElement("mul:BS");
root.appendChild(child);
/* Add text element to the child */
text = doc.createTextNode("ENV3");
child.appendChild(text);
SOAPHeaderElement element = new SOAPHeaderElement("" ,"soapenv:Header" , doc);
element.setActor(null);
port.setHeader(element);
System.out.println(port.getHeaders()[0]);
port.getContactRelatedInfoOperation(parameters);
} catch (Exception e){
e.printStackTrace();
}
}
但我不知道为什么,也不知道如何最终得到一条包含我不想要的属性的消息。 例如当前代码的输出消息为:
<soapenv:Header soapenv:mustUnderstand="0" xsi:type="ns1:Document"
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:ns1="http://xml.apache.org/xml-soap">
<mul:MultiTenant xmlns:mul="">
<mul:OpCo xmlns:mul="">DENMARK</mul:OpCo>
<mul:BS xmlns:mul="">ENV3</mul:BS>
</mul:MultiTenant></soapenv:Header>
例如mul:OpCo
标签中的xmlns:mul=""
属性。 有没有办法删除该属性?
I'm trying to send a SOAP message.
I'm adding manually header to the message, using the next code:
public static void main(String[] args) {
try{
AmdocsServicesServiceagentLocator locator = new AmdocsServicesServiceagentLocator();
PortTypeEndpoint1BindingStub port = new PortTypeEndpoint1BindingStub(new URL("http://srvp7rd-tibco.rnd.local:8025/Process/SoapRequests/Amdocs-Services.serviceagent/PortTypeEndpoint1"),locator);
GetContactRelatedInfo parameters = new GetContactRelatedInfo();
GetContactRelatedInfoRequest request = new GetContactRelatedInfoRequest();
request.setPersonID("6610782925");
request.setPersonIDType("ID number (CPR)");
/* Creating an empty XML Document - We need a document*/
DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = dbfac.newDocumentBuilder();
Document doc = docBuilder.newDocument();
/* Creating the XML tree */
/* Create the root element and add it to the document */
Element root = doc.createElement("mul:MultiTenant");
doc.appendChild(root);
/* Adding the child to the root */
Element child = doc.createElement("mul:OpCo");
root.appendChild(child);
/* Add text element to the child */
Text text = doc.createTextNode("DENMARK");
child.appendChild(text);
/* Adding the child to the root */
child = doc.createElement("mul:BS");
root.appendChild(child);
/* Add text element to the child */
text = doc.createTextNode("ENV3");
child.appendChild(text);
SOAPHeaderElement element = new SOAPHeaderElement("" ,"soapenv:Header" , doc);
element.setActor(null);
port.setHeader(element);
System.out.println(port.getHeaders()[0]);
port.getContactRelatedInfoOperation(parameters);
} catch (Exception e){
e.printStackTrace();
}
}
But I don't know why, or how I'm ending up with a message including attributes that i didn't wanted.
For example the output message of the current code is:
<soapenv:Header soapenv:mustUnderstand="0" xsi:type="ns1:Document"
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:ns1="http://xml.apache.org/xml-soap">
<mul:MultiTenant xmlns:mul="">
<mul:OpCo xmlns:mul="">DENMARK</mul:OpCo>
<mul:BS xmlns:mul="">ENV3</mul:BS>
</mul:MultiTenant></soapenv:Header>
For example, the xmlns:mul=""
attribute in the mul:OpCo
tag.
Is there a way to delete that attribute?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这些不是属性,而是命名空间声明。您正在使用
mul:
命名空间前缀创建元素,并且必须在某处定义该前缀。 Java 添加了一个默认的空声明 (xmlns:mul=""
),以便您的 XML 最终格式良好 - 您不能在没有声明的情况下使用前缀。如果您不需要这些声明,请删除
mul:
前缀,或在文档中的其他位置正确定义它。不过,您还没有告诉我们您的文档应该是什么样子,因此很难建议您如何做到这一点。Those aren't attributes, those are namespace declarations. You're creating elements with the
mul:
namespace prefix, and that prefix has to be defined somewhere. Java is adding a default empty declaration (xmlns:mul=""
) just so that your XML ends up being well-formed - you can't use a prefix without declaring it.If you don't want those declarations, then remove the
mul:
prefix, or define it properly elsewhere in the document. You haven't told us what your document should look like, though, so it's hard to advise you how to do that.您的消息没有
mul
命名空间的声明。添加它,奇怪的xmlns:mul
属性就会消失。更新
现在我明白了,您只需创建肥皂消息的片段。这只是标头,
mul
命名空间可以在其他 SOAP-Envelope 元素上声明。您需要知道
mul
的命名空间(-name),仔细检查soapUI中的完整SOAP消息并仔细检查文档。然后在doc
上声明命名空间。稍后,如果外部元素以完全相同的方式声明mul
,则这些属性应该从序列化的 xml 中消失。You message doesn't have a declaration of the
mul
namespace. Add it, and the strangexmlns:mul
attributes should go away.Update
Now I understand, you just create a fragment of a soap message. This is just the header and the
mul
namespace may be declared on the other SOAP-Envelope element.You need to know the namespace(-name) of
mul
, double check the full SOAP message in soapUI and double-check the documentation. Then declare the namespace ondoc
. Later, if the outer element declaresmul
exactly the same way, the attributes should disappear from the serialized xml.