我已经编写了一个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>";


方法原型是:
公共布尔addPerson(字符串fName,字符串lName,字符串性别,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中传递参数的方式有问题。请提出建议。

问候,
阿奇夫

最佳答案

您尚未定义xsixsd命名空间。尝试类似以下的操作(但请参阅以下有关正确命名空间的注释的注释):

<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的规范(无论您使用的是哪个规范)以获取确定的名称空间!

07-28 02:03
查看更多