WEBSERVICE with AXIS2

Axis2+tomcat6.0 implements webService server-side publishing and client-side calling.

The first step: first download the jar package needed for development

Download: axis2-1.6 .1-war.zip

http://www.apache.org/dist//axis/axis2/java/core/1.6.1/

After downloading, unzip it to the webapps folder under the tomcat installation directory. After starting tomcat, the axis2 folder will be generated under the webapps directory.

Visit http://localhost:8080/axis2/ and you can see the following page, indicating that axis2 runs successfully.

Step 2:

Create a new Web Project under Eclipse, the project name: webServe. Create a new package com.cm.service, a 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";} }

Modify the web.xml file in the WEB-INF directory, the content is as follows:

    index .jsp 

Copy the modules, service and conf files under webapps/axis2/WEB-INF under the tomcat installation directory to the WEB-INF directory under HelloWorld. Copy the following jar packages under lib. Then create a new HelloWorld/META-INF path under services, and create services.xml under META-INF, with the following content:

  HelloWorld Service Example   com.cm.service.HelloWorld           

After starting tomcat, visit http://127.0.0.1:8080/webServe/services/HelloWorld? Wsdl can see service information.

So far the Axis2 WebService service has been successfully released

Axis2 client call:

Let’s take a look at an example of using the axis2 client to call

package com.cm.client;import javax.xml.namespace.QName;import org.apache.axis2.addressing.EndpointReference;import org.apache.axis2.client.Options;impor t 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(xmlStr,url,method);} public static String sendService(String xmlStr,String url,String method){ String xml=null; try {RPCServiceClient serviceClient = new RPCServiceClient(); Options options = serviceClient.getOptions(); EndpointReference targetEPR = new EndpointReference(url); options.setTo(targetEPR); // When creating a QName object, the first parameter of the construction method of the QName class Represents the namespace name of the WSDL file, that is, the targetNamespace attribute value of the  element QName opAddEntry = new QName("http://service.cm.com",method); // Parameters, if there are more than one, You can continue to add later, without specifying the name of the parameter Object[] opAddEntryArgs = new Object[] {xmlStr}; // Return parameter type, this is a bit different from axis1 // The invokeBlocking method has three parameters, the first parameter is The type is a QName object, which represents 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[]; // The third parameter represents the Class of the return value type of the WebService method Object, the parameter type is Class[]. // When the method has no parameters, the second parameter value of the invokeBlocking method cannot be null, but new Object[]{} must be used. // If the invoked WebService method does not return a value, the invokeRobust method of the RPCServiceClient class should be used, / / This method has only two parameters, and 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; }}

Run result:

Sorry,xiaoxu.wang. < /pre> 

Summary: The above is a simple case of Axis2 service publishing and invocation. As for other implementation methods, those who are interested can continue to study.

Leave a Comment

Your email address will not be published.