《Java调用以WSDL形式发布的web service》由会员分享,可在线阅读,更多相关《Java调用以WSDL形式发布的web service(3页珍藏版)》请在金锄头文库上搜索。
1、Java 调用以 WSDL 形式发布的 web service webservice 的 发布一般都是使用 WSDL(web service descriptive language)文件的样式来 发布的,在 WSDL 文件里面,包含这个 webservice 暴露在外面可供使用的接口。今天搜索 到了非常好的 webservice provider 列表 http:/ 70 多个包括很多方面的 free webservice provider,utilities-global weather 就 可以获取全球的天气预报。 下面我们来看 Java 如何通过 WSDL 文件来调用这些 web se
2、rvice: 注意,以下的代码并没有经过真正的测试,只是说明这些情况,不同版本的 Axis 相差很大, 大家最好以 apache 网站上的例子为准,这里仅仅用于说明其基本用法。 1,直接 AXIS 调用远程的 web service 我觉得这种方法比较适合那些高手,他们能直接看懂 XML 格式的 WSDL 文件,我自己是 看不懂的,尤其我不是专门搞这行的,即使一段时间看懂,后来也就忘记了。直接调用模 式如下: import java.util.Date; import java.text.DateFormat; import org.apache.axis.client.Call; impor
3、t 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 文件 /以下都是套路 Service
4、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.ax
5、is.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.apa
6、che.soap.util.xml.*; import org.apache.soap.*; import org.apache.soap.rpc.*;import java.io.*; import .*; import java.util.Vector;public class caService public static String getService(String user) URL url = null; try url=new URL(“http:/192.168.0.100:8080/ca3/services/caSynrochnized“); catch (Malform
7、edURLException mue) return mue.getMessage(); / This is the main SOAP object Call soapCall = new Call(); / Use SOAP encoding soapCall.setEncodingStyleURI(Constants.NS_URI_SOAP_ENC); / This is the remote object were asking for the price soapCall.setTargetObjectURI(“urn:xmethods-caSynrochnized“); / Thi
8、s is the name of the method on the above object soapCall.setMethodName(“getUser“); / We need to send the ISBN number as an input parameter to the method Vector soapParams = new Vector();/ name, type, value, encoding style Parameter isbnParam = new Parameter(“userName“, String.class, user, null); soa
9、pParams.addElement(isbnParam); soapCall.setParams(soapParams); try / Invoke the remote method on the object Response 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.getFa
10、ultString(); return f; else / read result Parameter soapResult = soapResponse.getReturnValue (); / get a string from the result return soapResult.getValue().toString(); catch (SOAPException se) return se.getMessage(); 3,使用 wsdl2java 把 WSDL 文件转成本地类,然后像本地类一样使用,即可。 这是像我这种懒人最喜欢的方式,仍然以前面的 global weather
11、report 为例。 首先 java org.apache.axis.wsdl.WSDL2Java http:/ 原本的网址是 http:/ Linux 下面它不能解析,所以去掉问号,改为点号。 那么就会出现 4 个文件: GlobalWeather.java GlobalWeatherLocator.java GlobalWeatherSoap.java GlobalWeatherSoapStub.java 其中 GlobalWeatherSoap.java 是我们最为关心的接口文件,如果你对 RMI 等 SOAP 实现的 具体细节不感兴趣,那么你只需要看接口文件即可,在使用的时候,引入这个接口即可, 就好像使用本地类一样。