一般问题:消费SOAP Web服务 - Java VS PHP
我们正在我们公司推出一个名为ServiceNow的新ITSM Toolsuite。 ServiceNow提供了很多不错的开箱即用Web服务。 当前我们正在实现一些与其他interal系统的接口,并且我们使用这些Webservices来使用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生成类..跆拳道?做什么的?
$ ./wsdl2java.sh -uri https://blah.com/incident.do?WSDL
复制生成的类Java项目在Eclipse。
- 使用此类:
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的网络服务向导,(它这个最适合你),但我真的没有广泛使用它们。 – madth3
@felixsigl希望你在这个时候为你的问题找到了解决方案。你能分享一下吗? 因为我也遇到了同样的问题,我发现Json Service作为替代解决方案。但是我在连接到ServiceNow时遇到了一些问题。如果你有一些代码片段,这将是有帮助的。 –