Java調用WebService的方法總結


1、使用命令wsimport自動生成java代碼

         wsimport是jdk自帶的,可以根據wsdl文檔生成客戶端調用代碼的工具.

    wsimport.exe位於JAVA_HOME\bin目錄下。

   常用參數為:
    •-d<目錄>  - 將生成.class文件。默認參數。
    •-s<目錄> - 將生成.java文件。
    •-p<生成的新包名> -將生成的類,放於指定的包下。
    •(wsdlurl) - http://server:port/service?wsdl,必須的參數。

   示例:

      wsimport –s . http://192.168.0.100/one?wsdl

  注意:-s不能分開,-s后面有個小點,用於指定源代碼生成的目錄。點即當前目錄。

  如果使用了-s參數則會在目錄下生成兩份代碼,一份為.class代碼。一份為.java代碼。

  .class代碼,可以經過打包以后使用。.java代碼可以直接Copy到我們的項目中運行。

項目示例

使用這種方法需要稍微能看懂WSDL,代碼和WDSL的對應關系如上圖所示。中間部分為自己要寫的代碼,左上角為源文件,除了Main.java其他的都市自動生成的。

 

 

 

2、使用AXIS調用WebService

     需要用到的jar包:   

  • axis-1_2/lib/axis.jar
  • axis-1_2/lib/jaxrpc.jar
  • axis-1_2/lib/saaj.jar
  • axis-1_2/lib/commons-logging.jar
  • axis-1_2/lib/commons-discovery.jar
  • axis-1_2/lib/wsdl4j.jar

示例代碼:

  個人理解:Axis根據WSDL獲取WebService的基本信息,然后程序員設置xml代碼和java代碼的對應關系,Axis自動發送請求和解析響應;

        因為在WebService中java類時經過序列化的,所以傳遞參數不是基本數據類型時,要指定序列化對應關系,就是代碼25、26行,這時候反序列化器可以為空;

        當然當返回消息為java對象時(java對象我一般用wsimport方法自動生成),要指定將XML反序列化成java對象的對應關系,就是代碼72、73行,這時序列化器可以為空;

  1 /**
  2      * 第二種方法:Axis方法調用
  3      */
  4     public static void axis_01() {
  5         try {
  6             String endpoint = "http://127.0.0.1:8089/BaoIntlWebService/ws/binding";
  7 
  8             /*
  9              * Create new Service and Call objects. These are the standard
 10              * JAX-RPC objects that are used to store metadata about the service
 11              * to invoke.
 12              */
 13             Service service = new Service();
 14             Call call = (Call) service.createCall();
 15 
 16             // Set up endpoint URL - this is the destination for our SOAP
 17             // message.
 18             call.setTargetEndpointAddress(new java.net.URL(endpoint));
 19 
 20             // 設置調用WS的方法
 21             call.setOperationName(new QName("http://webservice.baointl.com/", "mapTest"));
 22 
 23             // 注冊序列化/反序列化器
 24             QName qn_request = new QName("http://webservice.baointl.com/", "requestTest");
 25             call.registerTypeMapping(RequestTest.class, qn_request, new BeanSerializerFactory(
 26                     RequestTest.class, qn_request), new BeanDeserializerFactory(RequestTest.class,
 27                     qn_request));
 28 
 29             // 設置參數名
 30             call.addParameter("request", qn_request, RequestTest.class, ParameterMode.IN);
 31 
 32             // 設置返回類型
 33             call.setReturnType(org.apache.axis.Constants.XSD_STRING);
 34 
 35             RequestTest request = new RequestTest();
 36             request.setName("LEO");
 37             request.setAge(28);
 38 
 39             Object ret = call.invoke(new Object[] { request });
 40 
 41             System.out.println("got '" + ret + "'");
 42         } catch (RemoteException e) {
 43             e.printStackTrace();
 44         } catch (ServiceException e) {
 45             e.printStackTrace();
 46         } catch (MalformedURLException e) {
 47             e.printStackTrace();
 48         }
 49     }
 50 
 51     public static void axis_02() {
 52         try {
 53             String endpoint = "http://127.0.0.1:8089/BaoIntlWebService/ws/binding";
 54 
 55             /*
 56              * Create new Service and Call objects. These are the standard
 57              * JAX-RPC objects that are used to store metadata about the service
 58              * to invoke.
 59              */
 60             Service service = new Service();
 61             Call call = (Call) service.createCall();
 62 
 63             // Set up endpoint URL - this is the destination for our SOAP
 64             // message.
 65             call.setTargetEndpointAddress(new java.net.URL(endpoint));
 66 
 67             // 設置調用WS的方法
 68             call.setOperationName(new QName("http://webservice.baointl.com/", "push"));
 69 
 70             // 注冊序列化/反序列化器
 71             QName qn = new QName("http://webservice.baointl.com/", "pushRespMsg");
 72             call.registerTypeMapping(PushRespMsg.class, qn, null, new BeanDeserializerFactory(
 73                     PushRespMsg.class, qn));
 74 
 75             // 設置參數名:
 76             call.addParameter("username", // 參數名
 77                     org.apache.axis.Constants.XSD_STRING,// 參數類型:String
 78                     ParameterMode.IN);// 參數模式:'IN' or 'OUT'
 79             call.addParameter("segno", // 參數名
 80                     org.apache.axis.Constants.XSD_STRING,// 參數類型:String
 81                     ParameterMode.IN);// 參數模式:'IN' or 'OUT'
 82             call.addParameter("first", // 參數名
 83                     org.apache.axis.Constants.XSD_STRING,// 參數類型:String
 84                     ParameterMode.IN);// 參數模式:'IN' or 'OUT'
 85             call.addParameter("keyword1", // 參數名
 86                     org.apache.axis.Constants.XSD_STRING,// 參數類型:String
 87                     ParameterMode.IN);// 參數模式:'IN' or 'OUT'
 88             call.addParameter("keyword2", // 參數名
 89                     org.apache.axis.Constants.XSD_STRING,// 參數類型:String
 90                     ParameterMode.IN);// 參數模式:'IN' or 'OUT'
 91             call.addParameter("remark", // 參數名
 92                     org.apache.axis.Constants.XSD_STRING,// 參數類型:String
 93                     ParameterMode.IN);// 參數模式:'IN' or 'OUT'
 94             // 設置返回值類型
 95             call.setReturnClass(PushRespMsg.class);
 96 
 97             Object[] object = new Object[6];
 98             object[3] = "11111";
 99             object[4] = "22222";
100             object[5] = "mark";
101             PushRespMsg result = (PushRespMsg) call.invoke(object);
102             System.out.println(result.getStatus());
103             System.out.println(result.getMessage());
104             System.out.println(result.getSuccessNum());
105             System.out.println(result.getFialedNum());
106         } catch (RemoteException e) {
107             e.printStackTrace();
108         } catch (ServiceException e) {
109             e.printStackTrace();
110         } catch (MalformedURLException e) {
111             e.printStackTrace();
112         }
113     }

 3、使用AXIS2調用WebService

  3.1 Axis2 DataBinding Framework

  3.2 XMLBeans

  3.3 JiBX databinding


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM