java调用webservice接口方法

上传人:飞*** 文档编号:39930842 上传时间:2018-05-21 格式:DOCX 页数:4 大小:17.13KB
返回 下载 相关 举报
java调用webservice接口方法_第1页
第1页 / 共4页
java调用webservice接口方法_第2页
第2页 / 共4页
java调用webservice接口方法_第3页
第3页 / 共4页
java调用webservice接口方法_第4页
第4页 / 共4页
亲,该文档总共4页,全部预览完了,如果喜欢就下载吧!
资源描述

《java调用webservice接口方法》由会员分享,可在线阅读,更多相关《java调用webservice接口方法(4页珍藏版)》请在金锄头文库上搜索。

1、javajava 调用调用 webservicewebservice 接口方法接口方法webservice 的 发布一般都是使用 WSDL(web service descriptive language)文件的样式来发布的,在 WSDL 文件里面,包含这个 webservice 暴 露在外面可供使用的接口。今天搜索到了非常好的 webservice provider 列表http:/ 70 多个包括很多方面的 free webservice provider,utilities-global weather 就可以获取全球的天气预报。下面我们来看 Java 如何通过 WSDL 文件来调用这些

2、 web service:注意,以下的代码并没有经过真正的测试,只是说明这些情况,不同版本的 Axis 相差很大,大家最好以 apache 网站上的例子为准,这里仅仅用于说明其 基本用法。 1,直接 AXIS 调用远程的 web service我觉得这种方法比较适合那些高手,他们能直接看懂 XML 格式的 WSDL 文件,我 自己是看不懂的,尤其我不是专门搞这行的,即使一段时间看懂,后来也就忘 记了。直接调用模式如下:import java.util.Date; import java.text.DateFormat; import org.apache.axis.client.Call; i

3、mport org.apache.axis.client.Service; import javax.xml.namespace.QName; import java.lang.Integer; import javax.xml.rpc.ParameterMode;public class caClient public static void main(String args) try String endpoint = “http:/localhost:8080/ca3/services/caSynrochnized?wsdl“;/直接引用远程的 wsdl 文件/以下都是套路 Servic

4、e service = new Service();Call call = (Call) service.createCall();call.setTargetEndpointAddress(endpoint);call.setOperationName(“addUser“);/WSDL 里面描述 的接口名称call.addParameter(“userName“, org.apache.axis.encoding.XMLType.XSD_DATE,javax.xml.rpc.ParameterMode.IN);/ 接口的参数call.setReturnType(org.apache.axis

5、.encoding.XMLType.XSD_STRING);/设 置返回类型 String temp = “测试人员“;String result = (String)call.invoke(new Objecttemp);/给方法传递参数,并且调用方法System.out.println(“result is “+result);catch (Exception e) System.err.println(e.toString(); 2,直接 SOAP 调用远程的 webservice 这种模式我从来没有见过,也没有试过,但是网络上有人贴出来,我也转过来 import org.apache.

6、soap.util.xml.*; import org.apache.soap.*; import org.apache.soap.rpc.*;import java.io.*; import .*; import java.util.Vector;public class caServicepublic static String getService(String user) URL url = null;try url=new URL(“http:/192.168.0.100:8080/ca3/services/caSynrochnized“); catch (MalformedURLE

7、xception mue) return mue.getMessage();/ This is the main SOAP objectCall soapCall = new Call();/ Use SOAP encodingsoapCall.setEncodingStyleURI(Constants.NS_URI_SOAP_ENC);/ This is the remote object were asking for the pricesoapCall.setTargetObjectURI(“urn:xmethods-caSynrochnized“);/ This is the name

8、 of the method on the above objectsoapCall.setMethodName(“getUser“);/ We need to send the ISBN number as an input parameter to the methodVector soapParams = new Vector();/ name, type, value, encoding styleParameter isbnParam = new Parameter(“userName“, String.class, user, null);soapParams.addElement

9、(isbnParam);soapCall.setParams(soapParams);try / Invoke the remote method on the objectResponse soapResponse = soapCall.invoke(url,“);/ Check to see if there is an error, return “N/A“if (soapResponse.generatedFault() Fault fault = soapResponse.getFault();String f = fault.getFaultString();return f; e

10、lse / read resultParameter soapResult = soapResponse.getReturnValue ();/ get a string from the resultreturn soapResult.getValue().toString(); catch (SOAPException se) return se.getMessage(); 3,使用 wsdl2java 把 WSDL 文件转成本地类,然后像本地类一样使用,即可。 这是像我这种懒人最喜欢的方式,仍然以前面的 global weather report 为例。首先 java org.apache.axis.wsdl.WSDL2Java http:/ 原本的网址是 http:/ 个各问号,但是 Linux 下面它不能解析,所以去掉问号,改为点号。 那么就会出现 4 个文件: GlobalWeather.java GlobalWeatherLocator.java GlobalWeatherSoap.java GlobalWeatherSoapStub.java其中 GlobalWeatherSoap.java 是我们最为关心的接口文件,如果你对 RMI 等 SOAP 实现的具体细节不感兴趣,那么你只需要看接口文件即可,在使用的时候, 引入这个接口即可,就好像使用本地类一样。

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 办公文档 > 其它办公文档

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号