1. 程式人生 > >java調用webservice接口 幾種方法

java調用webservice接口 幾種方法

href default pst ted 網址 後來 即使 tool poi

webservice的 發布一般都是使用WSDL(web service descriptive language)文件的樣式來發布的,在WSDL文件裏面,包含這個webservice暴露在外面可供使用的接口。今天搜索到了非常好的 webservice provider列表

http://www.webservicex.net/WCF/default.aspx

這上面列出了70多個包括很多方面的free webservice provider,utilities->global weather就可以獲取全球的天氣預報。

下面我們來看Java如何通過WSDL文件來調用這些web service:

註意,以下的代碼並沒有經過真正的測試,只是說明這些情況,不同版本的Axis相差很大,大家最好以apache網站上的例子為準,這裏僅僅用於說明其基本用法。

1,直接AXIS調用遠程的web service

我覺得這種方法比較適合那些高手,他們能直接看懂XML格式的WSDL文件,我自己是看不懂的,尤其我不是專門搞這行的,即使一段時間看懂,後來也就忘記了。直接調用模式如下:

[java] view plain copy
  1. import java.util.Date;
  2. import java.text.DateFormat;
  3. import org.apache.axis.client.Call;
  4. import org.apache.axis.client.Service;
  5. import javax.xml.namespace.QName;
  6. import java.lang.Integer;
  7. import javax.xml.rpc.ParameterMode;
  8. public class caClient {
  9. public static void main(String[] args) {
  10. try {
  11. String endpoint = "http://localhost:8080/ca3/services/caSynrochnized?wsdl";
  12. // 直接引用遠程的wsdl文件
  13. // 以下都是套路
  14. Service service = new Service();
  15. Call call = (Call) service.createCall();
  16. call.setTargetEndpointAddress(endpoint);
  17. call.setOperationName("addUser");// WSDL裏面描述的接口名稱
  18. call.addParameter("userName",
  19. org.apache.axis.encoding.XMLType.XSD_DATE,
  20. javax.xml.rpc.ParameterMode.IN);// 接口的參數
  21. call.setReturnType(org.apache.axis.encoding.XMLType.XSD_STRING);// 設置返回類型
  22. String temp = "測試人員";
  23. String result = (String) call.invoke(new Object[] { temp });
  24. // 給方法傳遞參數,並且調用方法
  25. System.out.println("result is " + result);
  26. } catch (Exception e) {
  27. System.err.println(e.toString());
  28. }
  29. }
  30. }



2,直接SOAP調用遠程的webservice

這種模式我從來沒有見過,也沒有試過,但是網絡上有人貼出來,我也轉過來

[java] view plain copy
  1. import org.apache.soap.util.xml.*;
  2. import org.apache.soap.*;
  3. import org.apache.soap.rpc.*;
  4. import java.io.*;
  5. import java.net.*;
  6. import java.util.Vector;
  7. public class caService {
  8. public static String getService(String user) {
  9. URL url = null;
  10. try {
  11. url = new URL(
  12. "http://192.168.0.100:8080/ca3/services/caSynrochnized");
  13. } catch (MalformedURLException mue) {
  14. return mue.getMessage();
  15. }
  16. // This is the main SOAP object
  17. Call soapCall = new Call();
  18. // Use SOAP encoding
  19. soapCall.setEncodingStyleURI(Constants.NS_URI_SOAP_ENC);
  20. // This is the remote object we‘re asking for the price
  21. soapCall.setTargetObjectURI("urn:xmethods-caSynrochnized");
  22. // This is the name of the method on the above object
  23. soapCall.setMethodName("getUser");
  24. // We need to send the ISBN number as an input parameter to the method
  25. Vector soapParams = new Vector();
  26. // name, type, value, encoding style
  27. Parameter isbnParam = new Parameter("userName", String.class, user,
  28. null);
  29. soapParams.addElement(isbnParam);
  30. soapCall.setParams(soapParams);
  31. try {
  32. // Invoke the remote method on the object
  33. Response soapResponse = soapCall.invoke(url, "");
  34. // Check to see if there is an error, return "N/A"
  35. if (soapResponse.generatedFault()) {
  36. Fault fault = soapResponse.getFault();
  37. String f = fault.getFaultString();
  38. return f;
  39. } else {
  40. // read result
  41. Parameter soapResult = soapResponse.getReturnValue();
  42. // get a string from the result
  43. return soapResult.getValue().toString();
  44. }
  45. } catch (SOAPException se) {
  46. return se.getMessage();
  47. }
  48. }
  49. }

3,使用wsdl2java把WSDL文件轉成本地類,然後像本地類一樣使用,即可。

這是像我這種懶人最喜歡的方式,仍然以前面的global weather report為例。

首先 java org.apache.axis.wsdl.WSDL2Java http://www.webservicex.net/globalweather.asmx.WSDL

原本的網址是http://www.webservicex.net/globalweather.asmx?WSDL,中間個各問號,但是Linux下面它不能解析,所以去掉問號,改為點號。

那麽就會出現4個文件:

GlobalWeather.java

GlobalWeatherLocator.java

GlobalWeatherSoap.java

GlobalWeatherSoapStub.java
其中GlobalWeatherSoap.java是我們最為關心的接口文件,如果你對RMI等SOAP實現的具體細節不感興趣,那麽你只需要看接口文件即可,

在使用的時候,引入這個接口即可,就好像使用本地類一樣。

java調用webservice接口 幾種方法