用Java创建SOAP消息

时间:2018-10-24 11:51:13

标签: java http post soap wsdl

我是SOAP的新手,对此不太了解。这是我第一次必须使用肥皂。因此,我正在尝试发送此请求:

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Body>
    <tns:BatchUpload xmlns:tns="http://someurl.com/">
        <tns:data>
            <Batch xmlns="http://someurl.com/someAction">
                <Header>
                    <Identifier>55555</Identifier>
                    <Subscriber>Name</Subscriber>
                    <SubscriberUnit>Unit</SubscriberUnit>
                </Header>
                <Commands>
                    <Sequence>
                        <Command identifier="1">
                            <Indi>
                                <NID>223</NID>
                            </Indi>
                        </Command>
                    </Sequence>
                </Commands>
            </Batch>
        </tns:data>
    </tns:BatchUpload>
</SOAP-ENV:Body>

我的代码是:

       SOAPPart soapPart = soapMessage.getSOAPPart();

    String myNamespace = "tns";
    String myNamespaceURI = "http://someurl.com/";

    // SOAP Envelope
    SOAPEnvelope envelope = soapPart.getEnvelope();
    envelope.addNamespaceDeclaration(myNamespace, myNamespaceURI);

    // SOAP Body
    SOAPBody soapBody = envelope.getBody();
    SOAPElement soapBodyBatchUpload = soapBody.addChildElement("BatchUpload", myNamespace);
    soapBodyBatchUpload.addNamespaceDeclaration(myNamespace, myNamespaceURI);
    SOAPElement soapBodyData = soapBody.addChildElement("data", myNamespace);
    SOAPElement soapBodyBatch = soapBody.addChildElement("Batch", "", "http://someurl.com/someAction");
    SOAPElement soapBodyCommands = soapBody.addChildElement("Commands");
    SOAPElement soapBodyHeader = soapBodyBatch.addChildElement("Header");
    SOAPElement soapBodyHeaderIdentifier = soapBodyBatch.addChildElement("Identifier");
    SOAPElement soapBodyHeaderSubscriber = soapBodyBatch.addChildElement("Subscriber");
    SOAPElement soapBodyHeaderSubscriberUnit = soapBodyBatch.addChildElement("SubscriberUnit");
    SOAPElement soapBodySequence = soapBody.addChildElement("Sequence");
    SOAPElement soapBodyCommand = soapBody.addChildElement("Command");
    soapBodyCommand.addAttribute(QName.valueOf("Identifier"), "1");
    SOAPElement soapBodyIndi = soapBody.addChildElement("Indi");
    SOAPElement soapBodyNId = soapBody.addChildElement("NId");

    soapBodyNId.addTextNode("223");

    soapBodyHeader.addChildElement(soapBodyHeaderIdentifier);
    soapBodyHeader.addChildElement(soapBodyHeaderSubscriber);
    soapBodyHeader.addChildElement(soapBodyHeaderSubscriberUnit);
    soapBodyHeaderIdentifier.addTextNode("55555");
    soapBodyHeaderSubscriber.addTextNode("Name");
    soapBodyHeaderSubscriberUnit.addTextNode("Unit");
    soapBodyIndi.addChildElement(soapBodyNId);
    soapBodyCommand.addChildElement(soapBodyIndi);
    soapBodySequence.addChildElement(soapBodyCommand);
    soapBodyCommands.addChildElement(soapBodySequence);
    soapBodyBatch.addChildElement(soapBodyCommands);
    soapBodyData.addChildElement(soapBodyBatch);
    soapBodyBatchUpload.addChildElement(soapBodyData);

然后添加标题:

    MimeHeaders headers = soapMessage.getMimeHeaders();
    headers.addHeader("SOAPAction", soapAction);
    headers.addHeader("Identifier", "55555");
    headers.addHeader("Subscriber", "Name");
    headers.addHeader("SubscriberUnit", "Unit");

但这不会返回我想要的。我认为Commands元素从Batch继承了空的xmlns =“”。我正在寻找解决方案已有很长时间了。在这里发现了类似甚至可能相同的问题,但是没有一个答案可以解决我的问题。也许我找不到正确的答案。预先感谢。

0 个答案:

没有答案