【问题标题】:How to pass parameters through a SOAP Message to consume a parameterized method of a webservice如何通过 SOAP 消息传递参数以使用 Web 服务的参数化方法
【发布时间】:2012-01-26 20:31:28
【问题描述】:

我写了一个java SOAP WebService。还有一个消费者。如果我将 SOAP 消息发送到没有参数的方法。一切正常,收到了正确的响应。

但是,我无法使用具有参数的方法。我的 SOAP 消息以下列模式存储在以下字符串中。

 String xml =  "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"+
                            "<S:Envelope xmlns:S=\"http://schemas.xmlsoap.org/soap/envelope/\">"+
                                "<S:Header/>"+
                                "<S:Body>"+
                                    "<ns2:addPerson xmlns:ns2=\"http://service.cass.com/\">"+
                                        "<fName xsi:type=\"xsd:string\">vbn</fName>"+
                                        "<lName xsi:type=\"xsd:string\">yyyy</lName>"+
                                        "<gender xsi:type=\"xsd:string\">879</gender>"+
                                        "<age xsi:type=\"xsd:int\">90</age>"+
                                    "</ns2:addPerson>"+
                                "</S:Body>"+
                            "</S:Envelope>";

方法原型是: public boolean addPerson(String fName, String lName, String gender, int age);

我得到以下异常。

Exception in thread "main" java.io.IOException: Server returned HTTP response code: 500 for URL: http://localhost:8080/ServerSide/ws/personService
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1305)
    at com.cass.testRequest.makeSOAPRequest(testRequest.java:71)
    at com.cass.testRequest.main(testRequest.java:37)

请注意,如果我发送一个不带参数的 SOAPMessage,对于一个带 0 个参数的方法。一切正常,我得到了正确的回应。在我看来,我在 SOAPMessage 中传递参数的方式有问题。请建议如何做到这一点。

问候, 阿奇夫

【问题讨论】:

    标签: java web-services soap


    【解决方案1】:

    您尚未定义 xsixsd 命名空间。尝试以下操作(但请参阅下面的 cmets 以了解命名空间的正确版本):

    <S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/"  
      xmlns:xsi="http://www.w3.org/1999/XMLSchema-instance" 
      xmlns:xsd="http://www.w3.org/1999/XMLSchema">
    

    (没有参数的方法不需要这些,这就是它在这种情况下起作用的原因)。

    已编辑:以下内容在 http://validator.w3.org/check 处验证为正确的 XML

    <?xml version="1.0" encoding="UTF-8"?>
    <S:Envelope xmlns:S="http://www.w3.org/2003/05/soap-envelope"  
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
      xmlns:xsd="http://www.w3.org/2001/XMLSchema">
        <S:Header/>
        <S:Body>
            <ns2:addPerson xmlns:ns2="http://service.cass.com/">
            <fName xsi:type="xsd:string">vbn</fName>
            <lName xsi:type="xsd:string">yyyy</lName>
            <gender xsi:type="xsd:string">879</gender>
            <age xsi:type="xsd:int">90</age>
            </ns2:addPerson>
        </S:Body>
    </S:Envelope>
    

    虽然这并不意味着它符合 SOAP 架构...接下来要检查...

    例如,如果客户端使用 SOAP 1.1 而服务器使用 SOAP 1.2,您可能会遇到问题,因为我认为命名空间不同。同样,不要混合两个版本的命名空间——它们必须一致。

    而且我认为 xsd 和 xsi 的最新命名空间现在是 2001 年而不是 1999 年(我错了,我使用的是旧示例)。

    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    

    但请参阅 SOAP 1.1 或 1.2(无论您使用的是哪个)的规范以获取明确的命名空间!

    【讨论】:

    • 我添加了这个,但仍然没有运气:(
    • 首先,使用 XML 编辑器确保您的 XML 有效,以防您有任何难以用肉眼发现的不匹配的大括号等。然后看看您是否可以从服务器启用任何更详细的输出(日志记录等)以缩小问题范围。
    • 另外,请确保您在我建议的片段中正确地转义了引号!即\"
    • 服务器返回,“null”和“内部服务器错误”,我确实通过从函数返回真正的布尔值并评论其余代码但仍然没有运气:(请帮助
    • 当我像你一样发送消息时。使用较新的 xsi、xsd、xmlns 命名空间。服务器日志给了我。 “SOAP 1.2 消息在发送到仅限 SOAP 1.1 的端点时无效。”
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-12
    • 2015-09-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多