axis2+tomcat6.0 implements WebService server-side publishing and client invocation.
First step: First download the jar packages needed for development
Download: Axis2-1.6.1-war.zip
Http://www.apache.org/dist//axis/axis2/java/core/1.6.1/
After downloading and extracting to the WebApps folder under the Tomcat installation directory, after launching Tomcat, the Axis2 folder will be generated in the WebApps directory.
Access http://localhost:8080/axis2/can see the following pages indicating that the Axis2 run successfully.
Step Two:
Create a new Web project under Eclipse, project name: Webserve. New Package Com.cm.service, new class HelloWorld, the code is as follows:
Package Com.cm.service; Public class HelloWorld { public string SayHello (string name) {return "Hello", "+name+". " ;} Public String Saysorry (string name) {return "Sorry," +name+ "." ;} Public String Getworld () {return ' Hello,world ';} }
In the Web-inf directory, modify the Web. xml file to read as follows:
<?XML version= "1.0" encoding= "UTF-8"?><Web-appversion= "2.5"xmlns= "Http://java.sun.com/xml/ns/javaee"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xsi:schemalocation= "Http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <!--Axis2 config start -<servlet> <Servlet-name>Axisservlet</Servlet-name> <Servlet-class>Org.apache.axis2.transport.http.AxisServlet</Servlet-class> <Load-on-startup>1</Load-on-startup></servlet><servlet-mapping> <Servlet-name>Axisservlet</Servlet-name> <Url-pattern>/services/*</Url-pattern> </servlet-mapping> <!--Axis2 End - <welcome-file-list> <Welcome-file>index.jsp</Welcome-file> </welcome-file-list></Web-app>
Copy the modules, service, and Conf files under Webapps/axis2/web-inf in the Tomcat installation directory to the Web-inf directory under HelloWorld. Copy the following jar bag under Lib. Then services under the new Helloworld/meta-inf path, Meta-inf under the new Services.xml, the contents are as follows:
<Servicename= "HelloWorld"> <Description>HelloWorld Service Example</Description> <parametername= "ServiceClass">Com.cm.service.HelloWorld</parameter> <Operationname= "SayHello"> <Messagereceiverclass= "Org.apache.axis2.rpc.receivers.RPCMessageReceiver" /> </Operation> <Operationname= "Saysorry"> <Messagereceiverclass= "Org.apache.axis2.rpc.receivers.RPCMessageReceiver" /> </Operation> <Operationname= "Getworld"> <Messagereceiverclass= "Org.apache.axis2.rpc.receivers.RPCInOnlyMessageReceiver" /> </Operation> </Service>
Access http://127.0.0.1:8080/webServe/services/HelloWorld?wsdl can see the service information after starting Tomcat.
The WebService service to this AXIS2 has been successfully published
AXIS2 Client Calls:
Here's a look at using Axis2 client invoke instance
Packagecom.cm.client;ImportJavax.xml.namespace.QName;Importorg.apache.axis2.addressing.EndpointReference;Importorg.apache.axis2.client.Options;Importorg.apache.axis2.rpc.client.RPCServiceClient; Public classAxisutil { Public Static voidMain (string[] args) {String xmlstr= "Xiaoxu.wang"; String URL= "Http://127.0.0.1:8888/webServe/services/HelloWorld"; String Method= "Saysorry"; Axisutil.sendservice (Xmlstr,url,method); } Public Staticstring Sendservice (String xmlstr,string url,string method) {string XML=NULL; Try{rpcserviceclient serviceclient=Newrpcserviceclient (); Options Options=serviceclient.getoptions (); EndpointReference Targetepr=Newendpointreference (URL); Options.setto (TARGETEPR); //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, which is the targetnamespace property value of the <wsdl:definitions> elementQName Opaddentry =NewQName ("http://service.cm.com", method); //parameter, if there are multiple, continue to add to the back, do not specify the name of the parameterobject[] Opaddentryargs =Newobject[] {xmlstr}; //returns the parameter type, which is a little different from axis1//The invokeblocking method has three parameters, where the type of the first parameter is the 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[];//The third parameter represents the class object of the return value type of the WebService method, and the parameter type is class[]. //When a method has no arguments, the second parameter value of the Invokeblocking method cannot be null, and the new object[]{} is used//If the called WebService method does not return a value, the Invokerobust method of the Rpcserviceclient class should be used .//The method has only two parameters, and they have the same meaning as the first two parameters of the Invokeblocking methodClass[] Classes =NewClass[] {String.class }; XML= (String) serviceclient.invokeblocking (Opaddentry,opaddentryargs, classes) [0]; SYSTEM.OUT.PRINTLN (XML); } Catch(Exception e) {e.printstacktrace (); LongEnd =System.currenttimemillis (); } returnXML; }}
Operation Result:
Summary: The above is a simple case of AXIS2 Service release and invocation. As for other methods of implementation interested can continue to study.
Method of releasing WebService with AXIS2 (RPM)