如何通过 SOAP 消息传递参数以使用 Web 服务的参数化方法 [英] How to pass parameters through a SOAP Message to consume a parameterized method of a webservice

查看:32
本文介绍了如何通过 SOAP 消息传递参数以使用 Web 服务的参数化方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have written a java SOAP WebService. and a consumer as well. if I send a SOAP message to a method with no parameters. it works all fine, a proper response is recieved.

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

But, I am unable to consume a method that have parameters. My SOAP message is stored in following string in following pattern.

 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 sex, int age);

method prototype is: 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 中传递参数的方式有问题.请建议如何做到这一点.

Please notice that, if I send a SOAPMessage with no parameters, for a method with 0 parameters. It all works fine and I get a proper response. In my opinion, Something is wrong with the way I am passing parameters in SOAPMessage. Please suggest how to do that.

问候,阿基夫

推荐答案

您尚未定义 xsixsd 命名空间.尝试类似以下内容(但请参阅下面有关命名空间正确版本的评论):

You haven't defined the xsi or xsd namespaces. Try something like the following (but see comments below about correct veriosn of the namespaces):

<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">

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

(These wouldn't be needed for your method with no parameters, which is why it worked in that case).

已编辑:以下内容在 http://validator.w3 上验证为正确的 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 模式……那将是接下来要检查的事情……

though that doesn't mean it conforms to the SOAP schema...that would be the next thing to check...

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

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

And I think the up-to-date namespaces for xsd and xsi are now 2001 not 1999, (my mistake, I was using an old example).

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

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

But refer to the specs for SOAP 1.1 or 1.2 (whichever you are using) for the definitive namespaces!

这篇关于如何通过 SOAP 消息传递参数以使用 Web 服务的参数化方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆