Axis2 + tomcat6.0 implements WebService server publishing and client calling.
Step 1: first download the jar package required for development
Download: axis2-1.6.1-war.zip
Http://www.apache.org/dist//axis/axis2/java/core/1.6.1/
Decompress the downloaded files to the webapps folder under the tomcat installation directory. After Tomcat is started, the axis2 folder is generated under the webapps directory.
Access http: // localhost: 8080/axis2/. The following page is displayed, indicating that axis2 runs successfully.
Step 2:
Create a web project under eclipse with the project name webserve. Create the com. cm. service package and create the 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";}}
Modify the Web. xml file in the WEB-INF directory with the following content:
<?xml version="1.0" encoding="UTF-8"?><web-app version="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 the webapps/axis2/WEB-INF under the tomcat installation directory to the WEB-INF directory under helloworld. Copy the following jar package under Lib. Then create the helloworld/META-INF path under services, and create services. xml under the META-INF, the content is as follows:
<service name="HelloWorld"> <description> HelloWorld Service Example </description> <parameter name="ServiceClass"> com.cm.service.HelloWorld </parameter> <operation name="sayHello"> <messageReceiver class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" /> </operation> <operation name="saySorry"> <messageReceiver class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" /> </operation> <operation name="getWorld"> <messageReceiver class="org.apache.axis2.rpc.receivers.RPCInOnlyMessageReceiver" /> </operation> </service>
Start Tomcat and access http: // 127.0.0.1: 8080/webserve/services/helloworld? The service information is displayed in the WSDL.
The WebService of axis2 has been released successfully.
Axis2 client call:
The following describes how to call an instance using the axis2 client.
Package COM. cm. client; import javax. XML. namespace. QNAME; import Org. apache. axis2.addressing. endpointreference; import Org. apache. axis2.client. options; import Org. apache. axis2.rpc. client. rpcserviceclient; public class axisutil {public static void main (string [] ARGs) {string xmlstr = "Xiaoxu. wang "; string url =" http: // 127.0.0.1: 8888/webserve/services/helloworld "; string method =" saysorry "; axisutil. sendservice (x Mlstr, URL, method);} public static string sendservice (string xmlstr, string URL, string method) {string xml = NULL; try {rpcserviceclient serviceclient = new rpcserviceclient (); options = serviceclient. getoptions (); endpointreference targetepr = new endpointreference (URL); options. setto (targetepr); // when creating a QNAME object, the first parameter of the constructor of the QNAME class indicates the namespace name of the WSDL file, that is, <WSDL: definitions> the element's targetnamespace attribute value QNAME opaddent Ry = new QNAME ("http://service.cm.com", method); // parameter, if there are multiple, continue to add to the back, you do not need to specify the parameter name object [] opaddentryargs = new object [] {xmlstr}; // return the parameter type. This is a bit different from axis1. // The invokeblocking method has three parameters, the type of the first parameter is the QNAME object, indicating the method name to be called; // The second parameter indicates the parameter value of the WebService method to be called, and the parameter type is object []; // The third parameter indicates the Class Object of the WebService method's return value type. The parameter type is class []. // When the method does not have a parameter, the second parameter value of the invokeblocking method cannot be null. Instead, use new object [] {} // If the WebService method to be called has no return value, the invokerobust method of the rpcserviceclient class should be used. // This method has only two parameters, their meanings are the same as those of the first two parameters of the invokeblocking method. class [] classes = new class [] {string. class}; xml = (string) serviceclient. invokeblocking (opaddentry, opaddentryargs, classes) [0]; system. out. println (XML);} catch (exception e) {e. printstacktrace (); long end = system. currenttimemillis () ;}return XML ;}}
Running result:
Sorry,xiaoxu.wang.
Conclusion: The above is a simple case of axis2 Service Release and call. If you are interested in other implementation methods, you can continue to study.
Next blog: WebService and struts2 integration problems and solutions.