【发布时间】:2012-12-14 13:12:12
【问题描述】:
一般问题:
我们正在我们公司推出一个名为 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??
【问题讨论】:
-
我喜欢 Netbeans Web 服务向导,(它为您完成了大部分工作)但我真的没有广泛使用它们。
-
@felixsigl 希望你在这个时候找到了解决问题的方法。你能分享一下吗?因为我也遇到了同样的问题,我发现 Json Service 作为替代解决方案。但是我在连接到 ServiceNow 时遇到了一些问题。如果你有一些代码sn-ps,那会很有帮助。
标签: java php web-services soap servicenow