gpt4 book ai didi

java - 使用 Java Generic SOAPClient (SAAJ) 时抛出异常?

转载 作者:行者123 更新时间:2023-12-02 05:24:43 25 4
gpt4 key购买 nike

我正在尝试使用以下用于 Java 的 SOAPClient(详细信息是我从以下教程 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 请求来验证服务端点是否处于 Activity 状态,这令人惊讶地产生了正确的输出。

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 库值得推荐吗?

最佳答案

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

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

关于java - 使用 Java Generic SOAPClient (SAAJ) 时抛出异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11547908/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com