【问题标题】:wrong SOAP request getting created创建了错误的 SOAP 请求
【发布时间】:2015-04-20 16:20:43
【问题描述】:

这是我的代码正在创建的内容:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ws="http://example.com">
<soapenv:Header/>
    <soapenv:Body>
        <ws:isUserExists>
            <userId>10</userId>
        </ws:isUserExists>
    </soapenv:Body>
</soapenv:Envelope>

在我的 soapenv:信封 || xmlns:soapenv 来了两次

xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"

现在这就是我需要的: 这里xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" 不在 SOAP 请求中

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/ " xmlns:ws="http://example.com"> 
<soapenv:Header/> 
    <soapenv:Body> 
        <ws:isUserExists> 
            <userId>10</userId> 
        </ws:isUserExists> 
    </soapenv:Body> 
</soapenv:Envelope> 

我用来创建请求的 java 代码是:

public static SOAPMessage createIsUserExistsSOAPRequest(User user) throws SOAPException
    {
        MessageFactory messageFactory =MessageFactory.newInstance();
        SOAPMessage soapMessage = messageFactory.createMessage();
        soapMessage.getSOAPHeader().setPrefix("soapenv");
        SOAPPart soapPart = soapMessage.getSOAPPart();
        // SOAP Envelope
        SOAPEnvelope envelope = soapPart.getEnvelope();
        envelope.setPrefix("soapenv");
        envelope.addNamespaceDeclaration("ws","http://example.com");

        // SOAP Body
        SOAPBody soapBody = envelope.getBody();
        soapBody.setPrefix("soapenv");
        SOAPElement ensureUserExistsElem = soapBody.addChildElement("isUserExists", "ws");
        SOAPElement userIdElem = ensureUserExistsElem.addChildElement("userId");
        userIdElem.addTextNode(user.userId);
        String authorization = new sun.misc.BASE64Encoder().encode(("someid@gmail.com").getBytes());
        MimeHeaders hd = soapMessage.getMimeHeaders();
        hd.addHeader("Authorization", "Basic " + authorization);
        soapMessage.saveChanges();
        return soapMessage;
    }

请帮忙:我做错了什么?

【问题讨论】:

    标签: java web-services soap wsdl wsdl2java


    【解决方案1】:

    在我的soapenv中:信封|| xmlns:soapenv 来了两次

    它不会出现两次。您定义了 2 个绑定到同一个命名空间的前缀。


    xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"

    xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"


    请帮忙:我做错了什么?

    什么都没有。命名空间前缀是soapenvSOAP-ENV 还是polar-bear-ninjas 都没有关系。只要前缀绑定到名称空间,您就完全限定了该 XML 元素。两种 XML 都有效,使用哪个前缀无关紧要。

    【讨论】:

    • 真的,很像定义一个变量。然后你可以使用或不使用它。是多余的,但 XML 是有效的。
    • 非常感谢。在这里我还有一个疑问:我想从 standalone="no" /b>,或者我应该为这个疑问发布一个不同的问题?
    • 这很奇怪。您是指文档中的某处或应该处理此 XML 的代码中的 DTD 吗?这就是standalone变得有用的地方。创建另一个问题并在此处填写详细信息,因为我们需要问您几个问题才能得到答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-05-22
    • 1970-01-01
    • 2014-10-18
    • 1970-01-01
    • 2018-03-31
    • 2017-04-25
    • 1970-01-01
    相关资源
    最近更新 更多