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

标签 java web-services soap

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

方法原型(prototype)为: public boolean addPerson(String fName, String lName, String 性别, int 年龄);

我遇到了以下异常。

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(无论您使用哪个)的规范来了解明确的命名空间!

关于java - 如何通过 SOAP 消息传递参数以使用 Web 服务的参数化方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9024903/

相关文章:

java - 将 Java FX 2.0 添加到现有的 Netbeans 项目

Java 默认字符串哈希函数在单个字符串上产生冲突

ios - 检查是否有新内容上线

web-services - HTTP 响应 header - 应用程序版本

php - REST API - 为什么使用 PUT DELETE POST GET?

java - 在同一方法中使用准备好的语句时如何绕过或避免列更新?

java - 如何在 jFrame 上布局多个面板? ( java )

java - 斯坦福解析器在Web服务中的使用

c# - 在 C# 中反序列化 SOAP 数组

java - Axis 中的数组序列化