【问题标题】:SOAP Server was unable to process requestSOAP 服务器无法处理请求
【发布时间】:2015-05-30 16:03:57
【问题描述】:

我有一个关于 SOAP 请求的问题。我想解释一下我在做什么。 这是我的 SOAP 请求。

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:web="http://www.webserviceX.NET">
   <soapenv:Header/>
   <soapenv:Body>
       <web:GetWeather>
         <!--Optional:-->
         <web:CityName>Istanbul</web:CityName>
         <!--Optional:-->
         <web:CountryName>Turkey</web:CountryName>
       </web:GetWeather>
   </soapenv:Body>
</soapenv:Envelope>

端点:http://www.webservicex.net/globalweather.asmx

WSDL 链接:http://www.webservicex.net/globalweather.asmx?WSDL

这也是我的代码。

public static void main(String args[]) {
    try {
        // Create SOAP Connection
        SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory
                .newInstance();
        SOAPConnection soapConnection = soapConnectionFactory
                .createConnection();

        // Send SOAP Message to SOAP Server
        String url = "http://www.webservicex.net/globalweather.asmx?WSDL";
        // SOAPMessage soapResponse =
        SOAPMessage soapResponse = soapConnection.call(createSOAPRequest(),
                url);

        // Process the SOAP Response
        printSOAPResponse(soapResponse);
        soapConnection.close();
    } catch (Exception e) {
        System.err.println("Error occurred while sending SOAP Request to Server");
        e.printStackTrace();
    }
}

private static SOAPMessage createSOAPRequest() throws Exception {
    MessageFactory messageFactory = MessageFactory.newInstance();
    SOAPMessage soapMessage = messageFactory.createMessage();
    SOAPPart soapPart = soapMessage.getSOAPPart();

    String serverURL = "http://www.webserviceX.NET/";

    SOAPEnvelope envelope = soapPart.getEnvelope();
    envelope.addNamespaceDeclaration("web", serverURL);

    // SOAP Body
    SOAPBody soapBody = envelope.getBody();
    SOAPElement soapElement = soapBody.addChildElement("GetWeather", "web");
    SOAPElement soapElement1 = soapElement.addChildElement("CityName",
            "web");
    soapElement1.addTextNode("Istanbul");
    SOAPElement soapElement2 = soapElement.addChildElement("CountryName",
            "web");
    soapElement2.addTextNode("Turkey");

    MimeHeaders headers = soapMessage.getMimeHeaders();
    headers.addHeader("SOAPAction", serverURL + "GetWeather");
    soapMessage.saveChanges();
    return soapMessage;
}

/**
 * Method used to print the SOAP Response
 */
private static void printSOAPResponse(SOAPMessage soapResponse)
        throws Exception {
    TransformerFactory transformerFactory = TransformerFactory
            .newInstance();
    Transformer transformer = transformerFactory.newTransformer();
    Source sourceContent = soapResponse.getSOAPPart().getContent();
    System.out.print("\nResponse SOAP Message = ");
    StreamResult result = new StreamResult(System.out);
    transformer.transform(sourceContent, result);
}}

因此,我得到“服务器无法处理。程序或函数 'getWeather' 需要参数 '@CountryName',但未提供该参数。”

这是什么意思?我为什么要接受这个例外?

对解决方案有什么建议吗?

【问题讨论】:

    标签: soap fault


    【解决方案1】:

    您将变量 serverUrl 用作 HTTP 服务器 URL 和 XML 命名空间名称。它们很接近,但并不完全相同。命名空间名称是 http://www.webserviceX.NET,但您的服务器 URL 是 http://www.webserviceX.NET/(注意尾部斜杠)。 XML 命名空间的字符串必须与架构中的命名空间名称完全匹配。

    建议您为命名空间创建一个单独的变量(或直接内联):

       String serverURL = "http://www.webserviceX.NET/";
    
       SOAPEnvelope envelope = soapPart.getEnvelope();
       envelope.addNamespaceDeclaration("web", "http://www.webserviceX.NET");
       ...
    

    通过此更改,您的代码适用于我。

    【讨论】:

    • 感谢您的回复。我更改了我的代码作为您的答案并且它有效。我错过了我看不到的斜线。我正在使用 SoapUI 处理某些属性。您是否推荐任何有关 Web 服务的工具?
    • 我也使用 SoapUI。这是非常有帮助的。您可以尝试 Apache CXF wsdl2java (codegen)。它不一定比你正在做的更好,但它是另一种需要考虑的方法。此外,如果您还有其他 Java/SOAP 问题,请不要忘记标记为“Java”。 Java 追随者比 SOAP 追随者多得多
    • 谢谢你的一切。这些信息对我很有帮助。另外,我是新手,我将学习如何有效地使用stackovverflow。我会注意你的建议。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-03
    • 1970-01-01
    • 1970-01-01
    • 2016-06-09
    相关资源
    最近更新 更多