一个普遍的问题:
我们正在我们公司推出一个名为 ServiceNow 的新 ITSM 工具套件。ServiceNow 提供了很多很好的开箱即用的 Web 服务。目前我们正在实现一些与其他内部系统的接口,我们使用这些 Web 服务来使用 Servicenow 的数据。
我们是如何在PHP中做到的:
<?php
$credentials = array('login'=>'user', 'password'=>'pass');
$client = new SoapClient("https://blah.com/incident.do?WSDL", $credentials);
$params = array('param1' => 'value1', 'param1' => 'value1');
$result = $client->__soapCall('getRecords', array('parameters' => $params));
// result array stored in $result->getRecordsResult
?>
就是这样!5 分钟的工作,美丽而简单——从我的角度来看。
好的,现在在Java中也一样:
我做了一些研究,似乎每个人都在使用 Apache Axis2 来使用 Java 中的 Web 服务。所以我决定走这条路。
- 安装 Apache Axis
打开 cygwin 或 cmd 并从 WSDL 生成类 .. WTF?做什么的?
$ ./wsdl2java.sh -uri https://blah.com/incident.do?WSDL
将生成的类复制到 Eclipse 中的 Java 项目。
- 使用此类:
ServiceNow_incidentStub proxy = new ServiceNow_incidentStub();
proxy._getServiceClient().getOptions().setProperty(org.apache.axis2.transport.http.HTTPConstants.CHUNKED, Boolean.FALSE);
ServiceNow_incidentStub.GetRecords defectsGetRecords = new ServiceNow_incidentStub.GetRecords();
ServiceNow_incidentStub.GetRecordsResponse defectsResult = new ServiceNow_incidentStub.GetRecordsResponse();
proxy._getServiceClient().getOptions().setManageSession(true);
HttpTransportProperties.Authenticator basicAuthentication = new HttpTransportProperties.Authenticator();
basicAuthentication.setUsername("user");
basicAuthentication.setPassword("pass");
proxy._getServiceClient().getOptions().setProperty(org.apache.axis2.transport.http.HTTPConstants.AUTHENTICATE, basicAuthentication);
defectsResult = proxy.getRecords(defectsGetRecords);
com.service_now.www.ServiceNow_incidentStub.GetRecordsResult_type0[] defects = defectsResult.getGetRecordsResult();
for (int j=0; j < defects.length; j++) {
// do something
}
它的工作,但我认为这种方式非常复杂.. 每次 wsdl 中的某些内容发生变化 - 我必须使用轴重新编译它们。无法在全局范围内配置诸如 Soap-endpoint 之类的东西。
在 Java 中有没有更简单的方法来使用 WSDL 来使用 SOAP?