发送SOAP消息

时间:2012-02-08 14:45:23

标签: java xml 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>

例如,xmlns:mul=""标记中的mul:OpCo属性。 有没有办法删除该属性?

2 个答案:

答案 0 :(得分:1)

那些不是属性,那些是名称空间声明。您正在使用mul:命名空间前缀创建元素,并且必须在某处定义该前缀。 Java正在添加一个默认的空声明(xmlns:mul=""),以便您的XML最终形成良好 - 您无法在不声明前缀的情况下使用前缀。

如果您不想要这些声明,请删除mul:前缀,或在文档的其他位置正确定义。但是,您没有告诉我们您的文档应该是什么样的,因此很难建议您如何做到这一点。

答案 1 :(得分:1)

您的消息没有mul命名空间的声明。添加它,奇怪的xmlns:mul属性应该消失。

<强>更新

现在我明白了,你只需要创建一个soap消息片段。这只是标题,mul命名空间可以在另一个SOAP-Envelope元素上声明。

需要知道mul的命名空间(-name),仔细检查soapUI中的完整SOAP消息并仔细检查文档。然后在doc上声明命名空间。稍后,如果外部元素以完全相同的方式声明mul,则属性应从序列化的xml中消失。

相关问题