Axis2 3 ways to develop WebService clients _web

Source: Internet
Author: User
Tags addchild stub wsdl

First RPC method, no client code generated

Second, document mode, do not generate client code

Third, using the Wsdl2java tool, generate the client way to invoke Java code    package samples.quickstart.client;      import javax.xml.namespace.qname;   import org.apache.axiom.om.omabstractfactory;    import org.apache.axiom.om.omelement;   import org.apache.axiom.om.omfactory;    import org.apache.axiom.om.omnamespace;   import org.apache.axis2.axisfault;    import org.apache.axis2.addressing.endpointreference;   import  org.apache.axis2.client.options;   import org.apache.axis2.client.serviceclient;   import org.apache.axis2.rpc.client.rpcserviceclient;   import  samples.quickstart.stockquoteservicestub;   import samples.quickstart.xsd.getprice;   import samples.quickstart.xsd.getpriceresponse;      public class  stockquoteclient {        /**    *  method:     *  applying RPC to call   this way is equal to remote call,      *  that is to tell the remote server through the URL location, inform the method name, parameters and other,  call remote service, get results.      *  using the  org.apache.axis2.rpc.client.rpcserviceclient class to invoke webservice      *       "Note":                  If the called WebService method has return value   should use  invokeBlocking  method   This method has three parameters               the type of the first parameter is a QName object that represents the method name to invoke;              The second parameter represents the parameter value of the WebService method to invoke, and the parameter type is object[];                when the method has no parameters, the second parameter value of the Invokeblocking method cannot be null, but to use the new  object[]{}.              The third parameter represents the   return value type Class object for the WebService method. Parameter type is class[].   &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&Nbsp;                      If the invoked WebService method does not return a value   should use  invokeRobust  method               The method has only two parameters, meaning the same as the first two parameters of the Invokeblocking method.              When you create a QName object, the first parameter of the constructor method of the QName class represents the namespace name of the WSDL file,           is the targetnamespace property value of the  <wsdl:definitions> element.      *     */     public static void  Testrpcclient ()  {       try {          // axis1  Service-side   // string url =  "http://localhost:8080/StockQuote/ SERVICES/STOCKQUOTESERVICESOAP11PORT?WSDL ";         // axis2  Service-side          string url =  "HTTP://LOCALHOST:8080/AXIS2SERVERDEMO/SERVICES/STOCKQUOTESERVICE?WSDL";            //  calling webservice       using RPC    rpcserviceclient serviceclient = new rpcserviceclient ();         //  specify url   to invoke WebService        Endpointreference targetepr = new endpointreference (URL);          options options = serviceclient.getoptions ();          //determine target service address          options.setto (TARGETEPR);          //determine the invocation method           Options.setaction ("Urn:getprice");            /**          *  specifies the namespace of the GetPrice method and WSDL file to invoke          *  if   webservice  services driven by Axis2 authoring          *  namespaces   inconsistencies cause problems          * org.apache.axis2.axisfault: java.lang.runtimeexception:  Unexpected subelement arg0         */          qname qname = new qname ("Http://quickstart.samples/xsd",   "GetPrice");         //  Specify GetPrice method parameter value           Object[] parameters = new Object[] {  "a"  };                   //  Class object that specifies the data type of the GetPrice method return value          class[] returntypes =  new class[]&nbSp { double.class };            //  Calling method one   passing parameters , call the service, get the service to return the result set          OMElement element =  Serviceclient.invokeblocking (qname, parameters);         //It is noteworthy that The return result is an XML string encapsulated by the Omelement object.          //We can apply it flexibly, below I take the first element value and print it. Because the method invoked returns a result          String result =  Element.getfirstelement (). GetText ();         system.out.println (Result) ;            //  Call Method two  getprice method and output The return value of the method           object[] response = serviceclient.invokeblocking ( Qname, parameters, returntypes);         // String  r =  (String)  response[0];         Double r =  (Double)  response[0];    &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;SYSTEM.OUT.PRINTLN (R);           } catch  (axisfault e)  {          E.printstacktrace ();       }     }        /**     *  Method Two:  application document method call      *  The application of Ducument method is cumbersome and flexible. It's a lot more now. Because really get rid of the coupling we don't want      */     public static void  Testdocument ()  {       try {          // String url =  "Http://localhost:8080/axis2ServerDemo/services/StockQuoteService";          String url =  "http://localhost:8080/StockQuote/services/STOCKQUOTESERVICESOAP11PORT?WSDL ";            Options  Options = new options ();         //  Specifies the url         endpointreference targetepr = that invokes WebService  new endpointreference (URL);         options.setto (TargetEPR) ;         // options.setaction ("Urn:getprice");             serviceclient sender = new serviceclient ();          sender.setoptions (options);                             omfactory fac = omabstractfactory.getomfactory ();          string tns =&nbSP; " http://quickstart.samples/";         //  namespaces, sometimes namespaces don't add up, but it's best to add, Because sometimes things happen, you know          OMNamespace omNs =  Fac.createomnamespace (tns,  "");            omelement  method = fac.createomelement ("GetPrice",  omns);          omelement symbol = fac.createomelement ("symbol",  omns);          // symbol.settext ("1");          Symbol.addchild (Fac.createomtext (symbol,  "axis2 echo string "));          method.addchild (symbol);         method.build () ;                  omelement  result = sender.sendreceive (method);            system.out.println (Result);          } catch  (axisfault axisfault)  {          axisfault.printstacktrace ();       }     }       /**    *  for soap header construction validation information,     *  If your service side is not validated, then you do not need to add validation information in header     *    *  @param   serviceclient    *  @param  tns  namespaces     *  @param  user    *  @param  passwrod    */     public void  Addvalidation (serviceclient serviceclient, string tns , string user, string  passwrod)  {       OMFactory fac =  Omabstractfactory.getomfactory ();        omnamespace omns = fac.createomnamespace (tns,  "NSL");        omelement header = fac.createomelement ("AuthenticationToken ",  omns);       omelement ome_user = fac.createomelement (" Username ",  omns);       OMElement ome_pass =  Fac.createomelement ("Password",  omns);               ome_user.settext (user);       ome_pass.settext (passwrod);               header.addchild (ome_user);        header.addchild (ome_pass);          serviceclient.addheader (header );     }             /**      *  Method III: UsingAXIS2 plug-in generates client-side invocation      *     */     public static  void testcodeclient ()  {       try {          String url =  "http://localhost:8080/axis2ServerDemo/services/ StockQuoteService ";         stockquoteservicestub stub =  new stockquoteservicestub (URL);         GetPrice  Request = new getprice ();         request.setsymbol (" ABCD ");         GetPriceResponse response =  Stub.getprice (Request);         system.out.println (Response.get_return ( );       } catch  (org.apache.axis2.axisfault e)  {    &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; E.printstacktrace ();       } catch  (java.rmi.remoteexception e)  {         e.printstacktrace ();        }        }        public static void  main (String[] args)  {        stockquoteclient.testrpcclient ( );  // stockquoteclient.testdocument ();       //  Stockquoteclient.testcodeclient ();        }  }  

Wsdl2java is used to generate the appropriate server-side and client-code generation tools from WSDL.
The command-line format is: Wsdl2java [options]-uri <url or path>: a URL or path to a WSDL

For example:

Wsdl2java-uri http://localhost:8080/cxfService_0617/services/Hellows?wsdl-s-O build\client

The commonly used options are as follows:
-o <path>: Specify the output path of the generated code
-A: Code to generate asynchronous mode
-S: code that generates synchronous mode
-P <pkg>: code-specific PA Ckage name
-L <languange>: Language Used (JAVA/C) defaults to Java
-t: Generate test cases for code
-SS: Generate server-side code not generated by default
-SD: Generating service description Files Ser Vices.xml,-SS with the
-D <databinding>: Specify Databingding, for example, Adb,xmlbean,jibx,jaxme and Jaxbri
-G: Generate the server-side and client code
-PN <port_name>: Specify one of the ports when there is more than one port
-sn <serv_name>: Select a service in WSDL
-u : Expand the Data-binding class
-R <path>: Specify a repository
-ssi for code generation: Implementing Code Generation Interface classes for the server side
-S: Specifying the storage path for the generated source
-R: for the generated R esources Specifies the storage path
–nobuildxml: The Build.xml file is not generated in the output
–NOWSDL: The WSDL file
–nomessagereceiver is not generated in the resources directory: Do not generate the Messagereceiver class

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.