创建了错误的 SOAP 请求

wrong SOAP request getting created

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

<?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:Envelope || 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;
    }

请帮忙:我做错了什么?

In my soapenv:Envelope || xmlns:soapenv is coming twice

它没有出现两次。您正在定义 2 个绑定到同一命名空间的前缀。


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

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


Please help : what I am doing wrong?

没有。命名空间前缀是 soapenv 还是 SOAP-ENV 还是 polar-bear-ninjas 并不重要。只要前缀绑定到命名空间,您就完全限定了 XML 元素。两个 XML 都是有效的,使用哪个前缀并不重要。