生成Java Soap请求SAAJ API

时间:2019-05-24 07:38:08

标签: java xml soap saaj soap1.2

我必须连接到外部Web服务。我正在使用SAAJ api创建要发送的XML请求。
SAAJ api生成了一些我不知道如何摆脱的默认代码。 第一个示例是SAAJ API生成的XML,第二个XML正在正常工作

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope" xmlns="http://schemas.xmlsoap.org/soap/envelope/">
    <env:Body>
        <Login xmlns="*************">
            <password>[string?]</password>
        </Login>
    </env:Body>
</env:Envelope>
<Envelope xmlns="http://schemas.xmlsoap.org/soap/envelope/">
    <Body>
        <Login xmlns="*************">
            <password>[string?]</password>
        </Login>
    </Body>
</Envelope>


如您所见,我必须删除env:东西。...

我的代码:

        // Creating a new empty SOAP message object
        SOAPMessage reqMsg = MessageFactory.newInstance(
            SOAPConstants.SOAP_1_2_PROTOCOL).createMessage();

        // Populating SOAP body
        String prefix = "";
        String uri = "*********************";
        SOAPEnvelope envelope = reqMsg.getSOAPPart().getEnvelope();
        envelope.addNamespaceDeclaration("", "http://schemas.xmlsoap.org/soap/envelope/");
        SOAPBody body = envelope.getBody();

        SOAPBodyElement service = body.addBodyElement(
            envelope.createName("Login", prefix, uri));
        SOAPElement param = service.addChildElement(
            envelope.createName("password", prefix, uri));
        param.addTextNode(password);

        SOAPHeader header = envelope.getHeader();
        header.detachNode();
         // Connecting and calling
         printSOAPRequest(reqMsg);

         doTrustToCertificates();
     SOAPConnection con 
        = SOAPConnectionFactory.newInstance().createConnection();
         SOAPMessage response = con.call(reqMsg, "*****************************");
         con.close();

编辑: 有人告诉我该服务正在使用SOAP 1.2,但是在尝试使用SOAP 1.1进行连接(主要区别是xmlns=" xmlns="http://schemas.xmlsoap.org/soap/envelope/">")之后,该服务正常工作

0 个答案:

没有答案