我有这个示例代码:
private static final String endpoint = "https://www.***.**:443/WSEndUser?wsdl";
public static void main(String[] args) throws SOAPException {
SOAPMessage message = MessageFactory.newInstance().createMessage();
SOAPHeader header = message.getSOAPHeader();
header.detachNode();
/*
SOAPEnvelope envelope = message.getSOAPPart().getEnvelope();
envelope.setAttribute("namespace","namespaceUrl");
*/
SOAPBody body = message.getSOAPBody();
QName bodyName = new QName("getVServers");
SOAPBodyElement bodyElement = body.addBodyElement(bodyName);
SOAPElement symbol = bodyElement.addChildElement("loginName");
symbol.addTextNode("my login name");
symbol = bodyElement.addChildElement("password");
symbol.addTextNode("my password");
SOAPConnection connection = SOAPConnectionFactory.newInstance().createConnection();
SOAPMessage response = connection.call(message, endpoint);
connection.close();
SOAPBody responseBody = response.getSOAPBody();
SOAPBodyElement responseElement = (SOAPBodyElement)responseBody.getChildElements().next();
SOAPElement returnElement = (SOAPElement)responseElement.getChildElements().next();
if(responseBody.getFault()!=null){
System.out.println("1) " + returnElement.getValue()+" "+responseBody.getFault().getFaultString());
} else {
System.out.println("2) " + returnElement.getValue());
}
}
我得到了这个错误:
1) S:Client Cannot find dispatch method for {}getVServers
但我知道该方法存在...有什么问题吗?
最佳答案
如果您仍然有问题,请也发布 WSDL。
1) Web 服务调用失败,因为它找不到名为 getVServers
且命名空间为 {}
(空命名空间)的方法。
您的请求类似于:
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Body>
<getVServers>
<loginName>my login name</loginName>
<password>my password</password>
</getVServers>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
getVServers 在默认命名空间中。它应该是这样的,其中 namespace 应该是来自您的 WSDL 定义的 targetNamespace
:
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Body>
<ns:getVServers xmlns:ns="http://your-namespace-from-wsdl.com">
<loginName>my login name</loginName>
<password>my password</password>
</ns:getVServers>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
为了添加命名空间,请更改创建 bodyName 的方式:
QName bodyName = new QName("http://your-namespace-from-wsdl.com", "getVServers", "ns");
如果在您的 XML 架构上设置了 elementFormDefault="qualified"
或者如果 form="qualified"
出现在您的元素上。
2) 我认为您的 URL 端点不应包含 ?wsdl。
3) 您正在尝试连接到 HTTPS 网络服务。确保相应地设置证书和 DefaultSSLFactory。
关于java - 客户端找不到 {} 的分派(dispatch)方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14710917/