The first RPC method, does not generate client code
Second, the document method does not generate client code
Third, use the wsdl2java tool to generate client-side calls
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 one: * Calling this method in the way of rpc is equivalent to a remote call, * That is, tell the remote server through url location, inform the method name, parameters, etc., call the remote service, and get the result. * Use the org.apache.axis2.rpc.client.RPCServiceClient class to call the WebService * 【Note】: If the called WebService method has a return value, the invokeBlocking method should be used. This method has three parameters. The type of the first parameter is a QName object, indicating the name of the method to be called; The second parameter represents the parameter value of the WebService method to be called, and the parameter type is Object[]; When the method has no parameters, the second parameter value of the invokeBlocking method cannot be null, but use new Object[]{}. The third parameter represents the Class object of the return value type of the WebService method, and the parameter type is Class[]. If the called WebService method has no return value, the invokeRobust method should be used This method has only two parameters, which have the same meaning as the first two parameters of the invokeBlocking method. When creating a QName object, the first parameter of the constructor of the QName class represents the namespace name of the WSDL file, That is, the value of the targetNamespace attribute of the <wsdl:definitions> element. * */ public static void testRPCClient() { try { // axis1 服务端 // String url = "http://localhost:8080/StockQuote/services/StockQuoteServiceSOAP11port?wsdl"; // axis2 服务端 String url = "http://localhost:8080/axis2ServerDemo/services/StockQuoteService?wsdl"; // Use RPC to call WebService RPCServiceClient serviceClient = new RPCServiceClient(); // Specify the URL to call WebService EndpointReference targetEPR = new EndpointReference(url); Options options = serviceClient.getOptions(); // determine the target service address options.setTo(targetEPR); // determine the calling method options.setAction("urn:getPrice" ); /** * Specify the getPrice method to call and the namespace of the WSDL file * if the webservice server is written by axis2 * Problems caused by inconsistent namespaces * org.apache.axis2.AxisFault: java.lang.RuntimeException: Unexpected subelement arg0 */ QName qname = new QName("http://quickstart.samples/xsd", "getPrice" ); // Specify the parameter value of the getPrice method Object[] parameters = new Object[] { "13" }; // Specify the Class object of the data type of the return value of the getPrice method Class[] returnTypes = new Class[] { double . class }; // Call the method to pass the parameters, call the service, get the service and return the result set OMElement element = serviceClient.invokeBlocking(qname, parameters); // It is worth noting that the returned result is an xml string encapsulated by the OMElement object. // We can apply it flexibly, below I take the value of the first element and print it. Because the called method returns a result String result = element.getFirstElement().getText(); System.out.println(result); // Call the method 2 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 ]; System.out.println(r); } catch (AxisFault e) { e.printStackTrace (); } } /** * Method 2: Application document method call * The application in the way of ducument is now cumbersome and flexible. Used more now. Because really getting 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(); // 指定调用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 it's okay if the namespace doesn't increase, but it's better to add it, because sometimes something happens, 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(); } } /** * Construct authentication information for SOAP Header, * If your server is not verified, then you do not need to add verification information to the Header * * @param serviceClient * @param tns namespace * @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 3: Use the axis2 plugin to generate client-side calls * */ 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) { e.printStackTrace (); } catch (java.rmi.RemoteException e) { e.printStackTrace (); } } public static void main(String[] args) { StockQuoteClient.testRPCClient(); // StockQuoteClient.testDocument(); // StockQuoteClient.testCodeClient(); } }
wsdl2 java is used to generate the corresponding server and client code according to WSDL.
The command line format is: WSDL2 Java [options] -uri <url or path> : A url or path to a WSDL
E.g:
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 : Generate code in asynchronous mode
-s : Generate code in synchronous mode
-p <pkg> : Specify the package name of the code
-l <languange > : the language used ( Java/C) is java by default
-t : generate test cases for the code
-ss : generate server-side code without default
-sd : generate the service description file services.xml, only used with -ss
-d <databinding> : Specify databingding, for example, adb, xmlbean, jibx, jaxme and jaxbri
-g : Generate server and client code
-pn <port_name> : When there are multiple ports in WSDL, specify one of the ports
-sn < serv_name> : select a service in WSDL
-u : expand the data-binding class
-r <path> : specify a repository for code generation
-ssi : implement code generation interface class for the server
-S : specify storage for the generated source code path
-R : specify storage path for generated resources
--noBuildXML : do not generate build.xml file in output
--noWSDL : do not generate WSDL file in resources directory
--noMessageReceiver : Do not generate the MessageReceiver class