Axis Java WebService Test small example

Source: Internet
Author: User
Tags apache download string methods
From the official Apache download axis, I used axis2-1.2 this version, the latest is axis2-1.3 version, but 1.3 need JDK1.5 support, I am the JDk1.4, so I use axis2-1.2 as WS (Web Service) services.

Put the downloaded war package into Tomcat's WebApps directory, start the Tomcat service, and enter http://localhost:8080/axis2/in the browser address bar (this may be different depending on the tomcat configuration of each person), If the following interface appears, there is no problem with the WS service.


Write the Java WS service below
First write the server side, starting from the simplest Hello,world, the project is as follows:


Hello.java
package com;

 public class Hello {public
     String hw () {return
         "Hello,world";
     }
 }


Under the SRC build a meta-inf folder, create a Services.xml file, the contents of the file are as follows:

<service name= "Hello" >
     <Description>
         helloword example Description
     </Description>
     <parameter name= "ServiceClass" locked= "false" >com. hello</parameter>
     <operation name= "HW" >
     <messagereceiver class= " Org.apache.axis2.rpc.receivers.RPCMessageReceiver "/>
     </operation>
 </service>


In the XML description above, com. Hello refers to the class name you have established, HW is the corresponding method, and Hello is the corresponding service name.
Package this project as an AAR file (create a WS directory under D disk, then create COM and meta-inf folders under WS, copy Hello.class to COM directory, copy services.xml to wsmeta-inf/, Enter D:/WS through the Windows command line, run the command jar CVF Ws.aar *.*), run over and discover a Ws.aar file under the WS directory, and put the file in the Tomcat directory \webapp\axis2\web-inf\ Services directory, start the Tomcat service.

In the Address bar, enter: http://localhost:8080/axis2/services/listServices, if the service is normal, the service name of Hello in the active state will appear in the browser page. As illustrated:


Client Calls
Now I've used 2 ways to invoke

Client.java package com;
 Import org.apache.axiom.om.OMElement;
 Import Org.apache.axis.client.Call;
 Import Org.apache.axis.client.Service;
 Import org.apache.axis2.addressing.EndpointReference;
 Import org.apache.axis2.client.Options;

 Import org.apache.axis2.rpc.client.RPCServiceClient;
 Import Javax.xml.namespace.QName;
 Import javax.xml.rpc.ServiceException;
 Import java.net.MalformedURLException;
 Import java.rmi.RemoteException;

 Import Java.util.Iterator; public class Client {//public static void main (string[] args) throws RemoteException, Serviceexception, Malfo  rmedurlexception {/* **************** call Method 1 ***********************/rpcserviceclient rpcclient = new
         Rpcserviceclient ();
         Options opt = new options (); Opt.setto (New EndpointReference ("Http://localhost:8080/axis2/services/Hello")); Service address opt.setaction ("URN:HW");
         Methods Rpcclient.setoptions (OPT); omelement element = rpcclient.invokeblocking (NEW QName ("http://com", "HW"), new Object[]{null}); NULL indicates no parameter passing iterator values = Element.getchildrenwithname (New QName ("http://com", "return"));
             Return indicates a returned value while (Values.hasnext ()) {//traversal of the obtained data omelement omelement = (omelement) values.next ();
         System.out.println (Omelement.gettext ());
         }/* **************** call Method 2 ***********************/String methods = "HW";
         Service service = new service ();
         Call call = (call) Service.createcall ();
         Call.settargetendpointaddress (New Java.net.URL ("Http://localhost:8080/axis2/services/Hello"));
         Call.setoperationname (New QName ("http://com/", method));
         Call.setusesoapaction (TRUE);
         Call.setreturntype (Org.apache.axis.encoding.XMLType.XSD_STRING);
         Call.setsoapactionuri ("Http://com/GetServerList"); String k = (string) call.invoke (new object[]{}); Because the return value is of type string, the return value called here is also string type System.out.println (">&Gt;> "+k);
 return value Output}}


Test results:

>>>> Hello,world

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.