创建了错误的SOAP请求

时间:2015-02-19 10:50:08

标签: java web-services soap wsdl wsdl2java

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

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

请帮助:我做错了什么?

1 个答案:

答案 0 :(得分:4)

  

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

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


  

的xmlns:的 soapenv = “http://schemas.xmlsoap.org/soap/envelope/”

     

的xmlns:的 SOAP-ENV = “http://schemas.xmlsoap.org/soap/envelope/”


  

请帮助:我做错了什么?

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