使用ADB生成一个客户端
执行以下步骤来使用Axis Data Binding(ADB)构建一个客户端。
通过在Axis2_HOME/samples/quickstartadb目录键入以下命令来生成客户端数据绑定:
%AXIS2_HOME%/bin/WSDL2Java -uri resources/META-INF/StockQuoteService.wsdl -p samples.quickstart.clients -d adb -s
-o build/client
或者在Axis2_HOME/samples/quickstartadb目录简单的键入 ant generate.client。
下一步看看quickstartadb/src/samples/quickstart/clients/ADBClient.java,并看看它在Code Listing 10中是怎样定义的。
Code Listing 10:ADBClient类
该类使用你创建的Axis Data Bindings创建一个客户端存根。然后它在Web服务上调用getPrice和update操作。getPrice方法操作创建
GetPrice有效载荷并设置symbol为ABC。然后它发送请求并显示当前价格。update方法创建一个Update有效载荷,设置symbol为ABC及
price为42.35。
现在通过在Axis2_HOME/samples/quickstartadb目录键入 ant run.client来构建并运行客户端。
你应该得到以下输出:
使用XMLBeans生成一个客户端
执行以下步骤来使用XMLBeans数据绑定来构建一个客户端。
通过在xmlbeansClient目录键入以下命令来生成数据绑定:
%AXIS2_HOME%/bin/WSDL2Java -uri resources/META-INF/StockQuoteService.wsdl -p samples.quickstart.service.xmlbeans -d
xmlbeans -s -o build/client
或者简单的在Axis2_HOME/samples/quickstartxmlbeans目录下键入 ant generate.client。
注意这只会创建客户端存根代码而不会创建服务器端代码。
下一步看看quickstartxmlbeans/src/samples/quickstart/clients/XMLBEANSClient.java,并在Code Listing 11看看它怎样定义的
Code Listing 11:XMLBEANSClient类
执行以下步骤来使用Axis Data Binding(ADB)构建一个客户端。
通过在Axis2_HOME/samples/quickstartadb目录键入以下命令来生成客户端数据绑定:
%AXIS2_HOME%/bin/WSDL2Java -uri resources/META-INF/StockQuoteService.wsdl -p samples.quickstart.clients -d adb -s
-o build/client
或者在Axis2_HOME/samples/quickstartadb目录简单的键入 ant generate.client。
下一步看看quickstartadb/src/samples/quickstart/clients/ADBClient.java,并看看它在Code Listing 10中是怎样定义的。
Code Listing 10:ADBClient类
- package samples.quickstart.clients;
- import samples.quickstart.service.adb.StockQuoteServiceStub;
- public class ADBClient{
- public static void main(java.lang.String args[]){
- try{
- StockQuoteServiceStub stub =
- new StockQuoteServiceStub
- ("http://localhost:8080/axis2/services/StockQuoteService");
- getPrice(stub);
- update(stub);
- } catch(Exception e){
- e.printStackTrace();
- System.err.println("\n\n\n");
- }
- }
- /* fire and forget */
- public static void update(StockQuoteServiceStub stub){
- try{
- StockQuoteServiceStub.Update req = new StockQuoteServiceStub.Update();
- req.setSymbol ("ABC");
- req.setPrice (42.35);
- stub.update(req);
- System.err.println("done");
- } catch(Exception e){
- e.printStackTrace();
- System.err.println("\n\n\n");
- }
- }
- /* two way call/receive */
- public static void getPrice(StockQuoteServiceStub stub){
- try{
- StockQuoteServiceStub.GetPrice req = new StockQuoteServiceStub.GetPrice();
- req.setSymbol("ABC");
- StockQuoteServiceStub.GetPriceResponse res =
- stub.getPrice(req);
- System.err.println(res.get_return());
- } catch(Exception e){
- e.printStackTrace();
- System.err.println("\n\n\n");
- }
- }
- }
package samples.quickstart.clients; import samples.quickstart.service.adb.StockQuoteServiceStub; public class ADBClient{ public static void main(java.lang.String args[]){ try{ StockQuoteServiceStub stub = new StockQuoteServiceStub ("http://localhost:8080/axis2/services/StockQuoteService"); getPrice(stub); update(stub); } catch(Exception e){ e.printStackTrace(); System.err.println("\n\n\n"); } } /* fire and forget */ public static void update(StockQuoteServiceStub stub){ try{ StockQuoteServiceStub.Update req = new StockQuoteServiceStub.Update(); req.setSymbol ("ABC"); req.setPrice (42.35); stub.update(req); System.err.println("done"); } catch(Exception e){ e.printStackTrace(); System.err.println("\n\n\n"); } } /* two way call/receive */ public static void getPrice(StockQuoteServiceStub stub){ try{ StockQuoteServiceStub.GetPrice req = new StockQuoteServiceStub.GetPrice(); req.setSymbol("ABC"); StockQuoteServiceStub.GetPriceResponse res = stub.getPrice(req); System.err.println(res.get_return()); } catch(Exception e){ e.printStackTrace(); System.err.println("\n\n\n"); } } }
该类使用你创建的Axis Data Bindings创建一个客户端存根。然后它在Web服务上调用getPrice和update操作。getPrice方法操作创建
GetPrice有效载荷并设置symbol为ABC。然后它发送请求并显示当前价格。update方法创建一个Update有效载荷,设置symbol为ABC及
price为42.35。
现在通过在Axis2_HOME/samples/quickstartadb目录键入 ant run.client来构建并运行客户端。
你应该得到以下输出:
- 42
- done
42 done
使用XMLBeans生成一个客户端
执行以下步骤来使用XMLBeans数据绑定来构建一个客户端。
通过在xmlbeansClient目录键入以下命令来生成数据绑定:
%AXIS2_HOME%/bin/WSDL2Java -uri resources/META-INF/StockQuoteService.wsdl -p samples.quickstart.service.xmlbeans -d
xmlbeans -s -o build/client
或者简单的在Axis2_HOME/samples/quickstartxmlbeans目录下键入 ant generate.client。
注意这只会创建客户端存根代码而不会创建服务器端代码。
下一步看看quickstartxmlbeans/src/samples/quickstart/clients/XMLBEANSClient.java,并在Code Listing 11看看它怎样定义的
Code Listing 11:XMLBEANSClient类
- package samples.quickstart.clients;
- import samples.quickstart.service.xmlbeans.StockQuoteServiceStub;
- import samples.quickstart.service.xmlbeans.xsd.GetPriceDocument;
- import samples.quickstart.service.xmlbeans.xsd.GetPriceResponseDocument;
- import samples.quickstart.service.xmlbeans.xsd.UpdateDocument;
- public class XMLBEANSClient{
- public static void main(java.lang.String args[]){
- try{
- StockQuoteServiceStub stub =
- new StockQuoteServiceStub
- ("http://localhost:8080/axis2/services/StockQuoteService");
- getPrice(stub);
- update(stub);
- } catch(Exception e){
- e.printStackTrace();
- System.err.println("\n\n\n");
- }
- }
- /* fire and forget */
- public static void update(StockQuoteServiceStub stub){
- try{
- UpdateDocument reqDoc = UpdateDocument.Factory.newInstance();
- UpdateDocument.Update req = reqDoc.addNewUpdate();
- req.setSymbol ("ABC");
- req.setPrice (42.32);
- stub.update(reqDoc);
- System.err.println("done");
- } catch(Exception e){
- e.printStackTrace();
- System.err.println("\n\n\n");
- }
- }
- /* two way call/receive */
- public static void getPrice(StockQuoteServiceStub stub){
- try{
- GetPriceDocument reqDoc = GetPriceDocument.Factory.newInstance();
- GetPriceDocument.GetPrice req = reqDoc.addNewGetPrice();
- req.setSymbol("ABC");
- GetPriceResponseDocument res =
- stub.getPrice(reqDoc);
- System.err.println(res.getGetPriceResponse().getReturn());
- } catch(Exception e){
- e.printStackTrace();
- System.err.println("\n\n\n");
- }
- }
- }
package samples.quickstart.clients; import samples.quickstart.service.xmlbeans.StockQuoteServiceStub; import samples.quickstart.service.xmlbeans.xsd.GetPriceDocument; import samples.quickstart.service.xmlbeans.xsd.GetPriceResponseDocument; import samples.quickstart.service.xmlbeans.xsd.UpdateDocument; public class XMLBEANSClient{ public static void main(java.lang.String args[]){ try{ StockQuoteServiceStub stub = new StockQuoteServiceStub ("http://localhost:8080/axis2/services/StockQuoteService"); getPrice(stub); update(stub); } catch(Exception e){ e.printStackTrace(); System.err.println("\n\n\n"); } } /* fire and forget */ public static void update(StockQuoteServiceStub stub){ try{ UpdateDocument reqDoc = UpdateDocument.Factory.newInstance(); UpdateDocument.Update req = reqDoc.addNewUpdate(); req.setSymbol ("ABC"); req.setPrice (42.32); stub.update(reqDoc); System.err.println("done"); } catch(Exception e){ e.printStackTrace(); System.err.println("\n\n\n"); } } /* two way call/receive */ public static void getPrice(StockQuoteServiceStub stub){ try{ ...