Apache axis2 is a Web Services System Service and client implementation.
1. Download and deploy axis2.
1): http://axis.apache.org/axis2/java/core/download.cgi
Download the binary distribution and war distribution packages.
2. decompress the downloaded axis2-1.6.2-war.zip file and copy axis2.war to the webapps directory of Tomcat.
3) start Tomcat and access http: // localhost: 8080/axis2/in the browser. The following page is displayed, and the deployment is successful.
2. Create web services using common Java classes
1) Notes:This common Java class has no package..
2) create a pojo directory under the \ apache-Tomcat-6.0.35 \ webapps \ axis2 \ WEB-INF \ directory.
3) Java class.
/*** The first Web Services * @ author luxh */public class firstservice {Public String sayhello (string name) {string result = "hello," + name + ", this is first axis2 application! "; Return result;} Public String getinfo () {return" this is first axis2 application! ";}}
Copy the firstservice. Class file compiled for this class to the \ apache-Tomcat-6.0.35 \ webapps \ axis2 \ WEB-INF \ pojo directory without restarting tomcat. axis2 supports hot deployment.
4) Access: http: // localhost: 8080/axis2/services/listservices
We can see that the common Java class has been published as Web Services.
5) access web services
HTTP: /localhost: 8080/axis2/services/firstservice/getinfo
Http: // localhost: 8080/axis2/services/firstservice/is the address of Web Services, and getinfo is the method name.
Http: // localhost: 8080/axis2/services/firstservice/sayhello? Name = lihuai
Http: // localhost: 8080/axis2/services/firstservice/is the address of Web Services, and sayhello is the method name ,? Name = lihuai is the parameter name and parameter value
When passing parameters, note that the parameter name must be consistent with that in the method that provides the service. Public String sayhello (stringName)
3. the Java client uses RPC to call Web Services.
1) create a Java project, unzip axis2-1.6.2-bin.zip, decompress the package, and introduce all the jar files in the \ axis2-1.6.2 \ Lib \ directory to the classpath path of the project.
2) ClientCode
Package CN. luxh. WS. client; import javax. XML. namespace. QNAME; import Org. apache. axis2.axisfault; import Org. apache. axis2.addressing. endpointreference; import Org. apache. axis2.client. options; import Org. apache. axis2.rpc. client. rpcserviceclient; import Org. JUnit. test; public class firstclient {/*** call web services using the RPC Method * @ throws axisfault */@ testpublic void testgetservices () throws axisfault {// create a client rpcserviceclient client to access the service = new rpcserviceclient (); // obtain the owner options of the client Options = client. getoptions (); // set the address of the web services to be called string serviceepr = "http: // localhost: 8080/axis2/services/firstservice "; endpointreference EPR = new endpointreference (serviceepr); options. setto (EPR); // The default value is http://ws.apache.org/axis2/getinfobecause the common Java class of Web Services does not have a package "); // QNAME -- method called // new object [] {} -- passed parameter value // new class [] {} -- Return Value Type object [] result = client. invokeblocking (QNAME, new object [] {}, new class [] {string. class}); system. out. println ("returned result:" + result [0]); // call the sayhello method QNAME = new QNAME ("http://ws.apache.org/axis2", "sayhello"); Result = client. invokeblocking (QNAME, new object [] {"lihuai"}, new class [] {string. class}); system. out. println ("returned result:" + result [0]) ;}}
4. Use the stub generation method to call Web Services
You can use the wsdl2java command to generate the client code that calls Web Services.
Wsdl2java Command Format: wsdl2java-Uri http: // localhost: 8080/axis2/services/firstservice? WSDL-p cn. luxh. ws. Client-S-O stub
-Uri: Specifies the path of the Web Services WSDL file to be accessed.
-PThe generatedJavaClass package name
-OSpecifies the root directory for saving the generated file
1)in the command line into the axis2-1.6.2-bin.zip decompressed \ axis2-1.6.2 \ bin \ directory, because the wsdl2java command in this directory
2) Enter wsdl2java-Uri http: // localhost: 8080/axis2/services/firstservice? WSDL-P client-S-O stub and press ENTER
3) after execution, there is a stub folder under the \ axis2-1.6.2 \ bin \ directory, there is a firstservicestub under the SRC \ CN \ luxh \ ws \ Client package under the stub folder. java and firstserviceunsupportedencodingexceptionexception. java class. We can copy the corresponding packages in the two classes to directly call the Web Services method.
4) Stub client code
Package CN. luxh. WS. client; import Java. RMI. remoteException; import javax. XML. namespace. QNAME; import Org. JUnit. test; public class firstclient {@ testpublic void testgetservicebystub () throws RemoteException, firstserviceunsupportedencodingexceptionexception {firstservicestub firstservcestub = new firstservicestub (); // The wsdl2java command encapsulates the Web Services method into a static class // you need to obtain the returned result firstservicestub using the following method. getinfo = new firstservicestub. getinfo (); string ruselt = firstservcestub. getinfo (getinfo ). get_return (); system. out. println ("getinfo method return value:" + ruselt); firstservicestub. sayhello = new firstservicestub. sayhello (); sayhello. setname ("lihuai"); ruselt = firstservcestub. sayhello (sayhello ). get_return (); system. out. println ("Return Value of the sayhello method:" + ruselt );}}