简体   繁体   English

如何通过SOAP消息传递参数以使用Web服务的参数化方法

[英]How to pass parameters through a SOAP Message to consume a parameterized method of a webservice

I have written a java SOAP WebService. 我已经编写了一个Java SOAP WebService。 and a consumer as well. 以及消费者。 if I send a SOAP message to a method with no parameters. 如果我将SOAP消息发送给没有参数的方法。 it works all fine, a proper response is recieved. 一切正常,可以收到适当的响应。

But, I am unable to consume a method that have parameters. 但是,我无法使用具有参数的方法。 My SOAP message is stored in following string in following pattern. 我的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>";

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

and I am getting following exception. 我正在追随异常。

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)

Please notice that, if I send a SOAPMessage with no parameters, for a method with 0 parameters. 请注意,如果我发送不带参数的SOAPMessage,则表示参数为0的方法。 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. 我认为,我在SOAPMessage中传递参数的方式有问题。 Please suggest how to do that. 请提出建议。

regards, Aqif 问候,Aqif

You haven't defined the xsi or xsd namespaces. 您尚未定义xsixsd命名空间。 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). (没有参数的方法不需要这些,这就是在这种情况下它起作用的原因)。

Edited : The following validates as correct XML at http://validator.w3.org/check 编辑 :以下代码在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>

though that doesn't mean it conforms to the SOAP schema...that would be the next thing to check... 尽管这并不意味着它符合SOAP模式...这将是接下来要检查的内容...

You may get problems if the client is using SOAP 1.1 and server is using SOAP 1.2 for example as I think the namespaces are different. 例如,如果客户端使用SOAP 1.1,而服务器使用SOAP 1.2,则可能会出现问题,因为我认为名称空间是不同的。 Similarly, do not mix namespaces from the two versions - it must all be consistent. 同样,请勿混用两个版本中的名称空间-名称空间必须一致。

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). 而且我认为xsd和xsi的最新名称空间现在是2001年而不是1999年(我的错误,我使用的是旧示例)。

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

But refer to the specs for SOAP 1.1 or 1.2 (whichever you are using) for the definitive namespaces! 但是,请参阅SOAP 1.1或1.2的规范(无论您使用的是哪个规范)以获取确定的名称空间!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM