【问题标题】:Exception thrown when using Java Generic SOAPClient (SAAJ)?使用 Java Generic SOAPClient (SAAJ) 时抛出异常?
【发布时间】:2012-07-18 18:24:34
【问题描述】:

我正在尝试使用以下 SOAPClient for Java(我从以下教程 http://users.skynet.be/pascalbotte/rcx-ws-doc/saajpost.htm 中获得了详细信息)。

但是,它似乎正在抛出异常。

这是我的代码:

javax.xml.soap.SOAPMessage msg;
MessageFactory mf = MessageFactory.newInstance();
msg = mf.createMessage();
SOAPPart part = msg.getSOAPPart();
StreamSource source = new StreamSource(new File("samples/input1.xml”));
part.setContent(source);
msg.saveChanges();

String endpoint = "http://ws1.parasoft.com/glue/calculator";

SOAPConnectionFactory scf = SOAPConnectionFactory.newInstance();
SOAPConnection conn = scf.createConnection();
javax.xml.soap.SOAPMessage response = conn.call(msg, endpoint);
TransformerFactory tff = TransformerFactory.newInstance();
Transformer tf = tff.newTransformer();
Source sc = response.getSOAPPart().getContent();
ByteArrayOutputStream ostream = new ByteArrayOutputStream(); 
StreamResult result = new StreamResult(ostream);
tf.transform(sc, result);
conn.close();

System.out.println(new String(ostream.toByteArray(), "UTF-8”));

在此示例中,我们假设 samples/input1.xml 包含以下内容:

<s11:Envelope xmlns:s11="http://schemas.xmlsoap.org/soap/envelope/">
   <s11:Body>
      <ns1:add xmlns:ns1="http://www.parasoft.com/wsdl/calculator/">
         <ns1:x>248</ns1:x>
         <ns1:y>365</ns1:y>
      </ns1:add>
   </s11:Body>
</s11:Envelope>

我尝试使用的示例网络服务可以在这里找到: http://www.service-repository.com/client/operations

在运行上述 Java 代码时(使用 SOAPClient 库),抛出以下异常:

Jul 19, 2012 3:50:11 AM com.sun.xml.internal.messaging.saaj.client.p2p.HttpSOAPConnection post
SEVERE: SAAJ0008: Bad Response; cannot find /calculator
Exception in thread "main" com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl: com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl: Bad response: (404cannot find /calculator
    at com.sun.xml.internal.messaging.saaj.client.p2p.HttpSOAPConnection.call(HttpSOAPConnection.java:148)
    at corebus.test.deprecated.TestMain.main(TestMain.java:1870)
Caused by: com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl: Bad response: (404cannot find /calculator
    at com.sun.xml.internal.messaging.saaj.client.p2p.HttpSOAPConnection.post(HttpSOAPConnection.java:258)
    at com.sun.xml.internal.messaging.saaj.client.p2p.HttpSOAPConnection.call(HttpSOAPConnection.java:144)
    ... 1 more

CAUSE:

com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl: Bad response: (404cannot find /calculator
    at com.sun.xml.internal.messaging.saaj.client.p2p.HttpSOAPConnection.post(HttpSOAPConnection.java:258)
    at com.sun.xml.internal.messaging.saaj.client.p2p.HttpSOAPConnection.call(HttpSOAPConnection.java:144)

使用此网站,服务似乎已启动并运行,并且运行良好。

我什至通过执行一个简单的 cURL 请求验证了服务端点是否处于活动状态,这令人惊讶地产生了正确的输出。

curl -H "Content-Type: text/xml; charset=utf-8" -H "SOAPAction:http://www.parasoft.com/wsdl/calculator/" -d@soap-request.xml http://ws1.parasoft.com/glue/calculator

产生的输出是:

<soap:Envelope xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/‘>
    <soap:Body>
        <n:addResponse xmlns:n='http://www.parasoft.com/wsdl/calculator/‘>
            <n:Result xsi:type='xsd:float'>613.0</n:Result>
        </n:addResponse>
    </soap:Body>
</soap:Envelope>

所以,我的问题是:首先,Java 代码有什么问题?它怎么能被修复?或者,还有其他更好/更可靠的通用 SOAPClient 库可以推荐吗?

【问题讨论】:

    标签: java web-services soap wsdl saaj


    【解决方案1】:

    通过在浏览器的 url 中附加 ?wsdl 来查看 WSDL,或者以某种方式获取 WSDL。检查 WSDl 是否指定它是文档样式还是 PRC 样式,并在此基础上准备输入 xml。

    如果可能,请使用非常方便的 SOAP UI 工具。

    【讨论】:

    • 我一直在研究soapUI.. 但是,如果我没记错的话,与Java 集成所需的依赖项似乎很大。您(或任何人)是否有使用soapUI 作为Java Lib 的经验?
    猜你喜欢
    • 1970-01-01
    • 2013-05-24
    • 2013-11-17
    • 2012-07-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-09
    相关资源
    最近更新 更多