Three ways to develop Axis2 WebService client

RPC first embodiment, the client code is not generated

Second, document mode, the client code is not generated

Third, with wsdl2java tool to generate a client invoke

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 the StockQuoteClient {   
  
  / **  
   * Method a: 
   * rpc application of this manner is equivalent to invoke a remote call, 
   * tell i.e. remotely positioned url server, inform the method name and parameters, call the remote service, get the results. 
   * Use org.apache.axis2.rpc.client.RPCServiceClient class called WebService 
   * 
    [Note: 
     
        If the method is called WebService return value should use invokeBlocking method has three parameters which 
          type of the first parameter is a QName object represents to invoke the method name; 
          the second parameter represents a parameter value to be called WebService method, parameters of type Object []; 
            when the method takes no parameters, the second parameter value invokeBlocking measures can not be null, to use the new Object [] {}. 
          The third parameter indicates the return type WebService Class object methods, parameters of type Class [].
         
         
        If the method is called WebService no return value of the method using invokeRobust 
          which only two parameters, the first two parameters of the same meaning with their meaning invokeBlocking method. 
 
        When you create a QName object, the first parameter QName constructor of the class represented namespace name of the WSDL file 
        is <wsdl: definitions> targetNamespace attribute value of the element. 
   * 
   * /   
  Public  static  void testRPCClient () {  
     the try {  
       // Axis1 server  
 // String url = " HTTP: // localhost :? 8080 / the StockQuote / Services / StockQuoteServiceSOAP11port wsdl";  
       // Axis2 server   
      String url = "http ?: // localhost: 8080 / axis2ServerDemo / Services / wsdl the StockQuoteService " ;   
  
      // use RPC invoke WebService  
      The ServiceClient = RPCServiceClient new new RPCServiceClient ();  
       // specify Calling WebService the URL of   
      EndpointReference targetEPR = new new EndpointReference (url);   
      Options Options = serviceClient.getOptions ();  
       // determine the target service address   
      options.setTo (targetEPR);  
       // determine call methods   
      options.setAction ( "URN: getPrice" );   
  
      / **  
       * namespace getPrice method to be invoked and WSDL files 
       * If the webservice service end by axis2 written 
       question * namespace inconsistencies caused 
       * org.apache.axis2. AxisFault: java.lang.RuntimeException: Unexpected the SubElement arg0 
       * /  
      QName the QName = new new the QName ( "HTTP: //quickstart.samples/xsd", "getPrice" );  
       // specified parameter values getPrice method   
      Object [] = Parameters new new Object [] { "13 is" };   
        
      // specified getPrice the method return value data type class Object   
      class [] = returnTypes new new class [] { Double . class };   
  
      // call a method to pass parameters, service calls, the service returns a result set acquires   
      the OMElement Element = serviceClient.invokeBlocking (QName, parameters );  
       // worth noting that the result is returned by the package xml string section OMElement objects.  
      // we can for the flexible application, here I take the first element value, and print it. Because the method call returns a result   
      String result =element.getFirstElement () getText ();.   
      System.out.println (Result);   
  
      // call the method two getPrice method and outputs the return value of the method of the   
      Object [] Response = serviceClient.invokeBlocking (QName, Parameters, returnTypes);  
       / / String R & lt = (String) Response [0];   
      Double R & lt = (Double) Response [0 ];   
      System.out.println (R & lt);   
  
    } the catch (The AxisFault E) {   
      e.printStackTrace ();   
    }   
  }   
  
  / **  
   * method two: call the application document (build request message) 
   * application now with ducument way to tedious and flexible. Now with more. Because really we do not want to get rid of a couple of 
   * /   
  public  static  void testDocument () {  
     the try {  
      // String url = "http://localhost:8080/axis2ServerDemo/services/StockQuoteService";  
      String url = "http://localhost:8080/StockQuote/services/StockQuoteServiceSOAP11port?wsdl";  
  
      Options options = new Options();  
      // 指定调用WebService的URL  
      EndpointReference targetEPR = new EndpointReference(url);  
      options.setTo(targetEPR);  
      // options.setAction("urn:getPrice");  
  
      ServiceClient sender = new ServiceClient();  
      sender.setOptions(options);  
        
        
      OMFactory fac =OMAbstractFactory.getOMFactory ();   
      String TNS = "HTTP: //quickstart.samples/" ;  
       // namespace, sometimes namespace does not increase the fine, but the best plus, because sometimes something happens, you know 
       // first namespace address parameter, the second parameter is the namespace prefix 
      OMNamespace omNs = fac.createOMNamespace (TNS, "" );  
       // The first parameter is to build the tag name, the second parameter is the namespace 
      OMElement method = fac.createOMElement ( "the getPrice" , omNs);   
      the OMElement Symbol = fac.createOMElement ( "Symbol" , omNs);
       // tag value provided   
      symbol.setText ( "13 is" );  
       // symbol.addChild (fac.createOMText (Symbol, "the Axis2 Echo String ")); 
      // add a sub-label
      method.addChild (Symbol);  
       // Construction packet 
      method.build ();  
       // send message request returns   
      the OMElement Result = sender.sendReceive (Method);   
  
      System.out.println (Result);   
  
    } the catch (The AxisFault AxisFault) {   
      axisFault.printStackTrace ();   
    }   
  }   
  
 / **  
  * SOAP Header configured as verification information, 
  * if you server is not verified, then you do not increase the authentication information in the Header 
  * 
  * @param the ServiceClient 
  * @param TNS named space 
  * @param the 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);   
  }   
  
    
  / **  
   * three methods: using a plug-in generator axis2 clients invoke 
   * 
   * /   
  public  static  void testCodeClient () {  
     the try {   
      String URL = "HTTP: // localhost: 8080 / axis2ServerDemo / Services / the StockQuoteService " ;   
      StockQuoteServiceStub Stub = new new StockQuoteServiceStub (URL);   
      the GetPrice Request = new new the GetPrice ();   
      request.setSymbol ( " ABCD " );   
      GetPriceResponse Response = stub.getPrice (Request);   
      System.out.println (response.get_return ());   
    }catch (org.apache.axis2.AxisFault e) {  
      e.printStackTrace();  
    } catch (java.rmi.RemoteException e) {  
      e.printStackTrace();  
    }  
  
  }  
  
  public static void main(String[] args) {  
     StockQuoteClient.testRPCClient();  
// StockQuoteClient.testDocument();  
    // StockQuoteClient.testCodeClient();  
  
  }  
}  

wsdl2 Java for generating a corresponding server and client code generation tool according to WSDL.

 wsdl2java.bat command can be found at <Axis2 installation directory> / bin directory.
 Before using the command wsdl2java.bat AXIS2_HOME need to set the environment variable, which value is <Axis2 installation directory>.
 WebService code in the Windows command line console output to generate the following call, for example:
 % AXIS2_HOME% \ bin \ WSDL2Java -uri  HTTP: // localhost: 8080 / Axis2 / Services / the HelloService wsdl?  -P -s -o Client Stub
 -url parameter specifies the path where the wsdl file can be a local path, or a network path.
 -p parameter specifies the names of the Java classes generated, -o parameter specifies the generation of a series of documents saved in the root directory.
 After completion of the implementation of the above command, you will find more than in the current directory of a stub directory,
 you can find a HelloServiceStub.java file stub / src / client directory,
 the file is complex call WebService, you can use this class directly in the program

 

Which are commonly used options as follows:
-o <path>: Specifies the output path of the generated code
-a: Asynchronous mode generated code
-s: generating a synchronization pattern code
-p <pkg>: Specifies the name of the package code
-l <languange >: the language used ( the Java / C) the default is the Java
-t: generate test cases for the code
-ss: server-side code generated by default does not generate
-sd: generation service description file services.xml, only be used in conjunction with -ss
-d <databinding>: Specifies databingding, e.g., the adb, XMLBean, JiBX, and JaxMe JaxBRI
-g: generating service and client code
-pn <port_name>: when there are multiple WSDL port, wherein a specified Port
-sn < serv_name>: selection of a WSDL-service
-u: expand the class of data-binding
-r <path>: code for generating a designated Repository
the -ssi: interface class code generation to implement server
-S: Specifies the source for the generation of memory path
-R: resources for the specified storage path generation
-noBuildXML: build.xml file does not generate output
-noWSDL: not generate the WSDL file in the resources directory
-noMessageReceiver: Class not generated MessageReceiver

 

Reprinted: https: //blog.csdn.net/zhangmeng_07/article/details/54694277

Published 407 original articles · won praise 2 · Views 6795

Guess you like

Origin blog.csdn.net/qq_29860591/article/details/104788723