WebService Summary (iv)--use Axis2 to publish and Invoke WebService

Source: Internet
Author: User

Preparatory work

AXIS2 official website http://axis.apache.org/Download AXIS2 related information


Where the Axis2-1.6.2-bin.zip file contains all the jar files in the Axis2, the Axis2-1.6.2-war.zip file is used to publish WebService to the Web container. The last two are plug-ins for Axis2 in Eclipse.

Probably talk about how these files are used.

1, decompression axis2-1.6.2-bin.zip to any directory. Then, in Eclipse, press configure.


2. Extract the Axis2-1.6.2-war.zip file to any directory, place the Axis2.war file in the directory into the <tomcat installation directory >\webapps directory, and start Tomcat, Enter the following url:http://localhost:8080/axis2/in the browser address bar, such as the main page of the Axis2 to see the installation is successful. Such as:


3, Eclipse installs that two Axis2 plug-in, the concrete installation step here does not say, installs after eclipse in File--new--others, if displays the following screen, the installation succeeds.


Publish WebService using Axis2

In Eclipse, write a Java Pojo class--helloworldserverimp with the following code:

Package Com.test.server;public class Helloworldserverimp {public string SayHello (String username) {return username+ ": He Lloworld ";}}
Can I tell you that the coding work is all over?


Look at the publishing process:

file--"new--" Axis2 Service Archive


This is a direct skip


The output location is directly selected to the following directory in Tomcat, and finish is automatically published.


At this point you will see that the corresponding directory is more than the following file


Visit the URL: http://localhost:8080/axis2/services/listServices to see our published WebService.


At this point, the publication succeeds.

Client calls WebService

Using the AXIS2 implementation, the code is as follows

WSClient Code:

Package Com.test.server;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;public class WSClient {private Rpcserviceclient serviceClient;private Options options;private endpointreference targetepr;public wsclient (String endpoint) throws Axisfault {serviceclient = New Rpcserviceclient (); options = Serviceclient.getoptions (); targetepr = new EndpointReference (endpoint); O Ptions.setto (TARGETEPR);} Public object[] Invokeop (string targetnamespace, string opname,object[] Opargs, class<?>[] opreturntype) throws axisfault,classnotfoundexception {//Set the name of the operation QName opqname = new QName (targetnamespace, opname);//The operation requires that the parameter passed in is already given in the parameter, Here the direct incoming method calls return Serviceclient.invokeblocking (Opqname, Opargs, Opreturntype);} /** * @param args * @throws axisfault * @throws classnotfoundexception */public static void Main (string[] args) throws Axi Sfault,classnotfoundexception {//This is the WSDL address final String endpointreference = "http://localhost:8080/axis2/services/ Helloworldserver "; final String targetnamespace =" http://server.test.com "; WSClient client = new WSClient (endpointreference); String opname = "SayHello"; object[] Opargs = new object[] {"Haitao"}; class<?>[] Opreturntype = new class[] {string[].class};object[] response = Client.invokeop (targetnamespace, opNam E, Opargs,opreturntype); System.out.println (((string[) response[0]) [0]);}}

Of course, you can also use the CXF call, which is the same as the client for several blogs:

Package Com.test.client;import Org.apache.cxf.jaxws.endpoint.dynamic.jaxwsdynamicclientfactory;public Class helloworldclient {public static void main (string[] args) {jaxwsdynamicclientfactory DCF = Jaxwsdynamicclientfactory.newinstance (); Org.apache.cxf.endpoint.Client Client = Dcf.createclient ("http:// Localhost:9000/helloservice?wsdl ");        Object[] Objects;try {objects = Client.invoke ("SayHello", "Haitao");//Output call result System.out.println (objects[0].tostring ( ));} catch (Exception e) {e.printstacktrace ();}}}

So farUseAxis2Publish and InvokeWebService ended. Before touching Axis2, feel this thing quite mysterious, when you really realize, you will find, it also is so. However, Axis2 is far from this point, continue to learn it.

WebService Summary (iv)--use Axis2 to publish and Invoke WebService

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.