Ii. Simple WebService example of Axis2

Source: Internet
Author: User

1. Compile the server code of a simple WebService. The Code is as follows:

Code import java. util. random;/*** <B> function: </B> example of the WebService HelloWorld Service * @ author hoojo * @ createDate 03:35:06 * @ file HelloWorldService. java * @ package com. hoo. service * @ project Axis2WebService * @ blog http://blog.csdn.net/IBM_hoojo * @ email hoojo_@126.com * @ version 1.0 */public class HelloWorldService {public String sayHello (String name) {return name + "say: hello [axis2] ";} public int getAge (int I) {return I + new Random (). nextInt (100 );}}

Note that the preceding HelloWorldService does not have a package. Copy the class file of this class to the pojo folder of the axis2 WEB-INF directory of webapps under the tomcat directory. If no pojo directory exists, manually create one folder. Enter: http: // localhost: 8080/axis2/in the browser/

Click the link of Services to see the manually released HelloWorldService, or enter http: // localhost: 8080/axis2/services/listServices in the address bar of the browser.

You can see the WebService you just pasted.

Click the link to view the content of the wsdl file. There is a lot of content. If you have read the axis1.x introduction, you will know the approximate structure of the wsdl file.

The following explains why to put the class in the pojo folder. First let's take a look at [tomcat_home]/webapps/axis2/WEB-INF/conf/axis2.xml

This file contains the following code:

<Deployer extension = ". class" directory = "pojo" class = "org. apache. axis2.deployment. POJODeployer"/>

Put the. class suffix file in the directory pojo directory.

2. Test the WebService method.

Copy the link address of the preceding HelloWorldService and add the method name and parameter value to test whether the call is successful. As follows:

Http: // localhost: 8080/axis2/services/HelloWorldService/sayHello? Name = jack

Http: // localhost: 8080/axis2/services/HelloWorldService this is the WebService address

/SayHello is the method name ,? Name = jack is the parameter name and Value

After entering the above address in the browser, you can see the following results:

The return value and method name are displayed. Ns: sayHelloResponse is the method name. All method names are followed by Response. The ns is of course the package name of the class where the current method is located, no package is the default axis2 domain name.

Similarly, the getAge method is the same as the call method.

Http: // localhost: 8080/axis2/services/HelloWorldService/getAge? I = 22

The result is as follows:

3. Let's take a look at the compilation of the client call code. The Code is as follows:

Code package com. hoo. service; 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;/*** <B> function: </B> the code for calling the HelloWorldService client * @ author hoojo * @ createDate 03:55:05 * @ file HelloWorldClient. java * @ package com. hoo. service * @ project Axis2WebService * @ blog http://blog.csdn.net/IBM_hoojo * @ email hoojo_@126.com * @ version 1.0 */public class HelloWorldClient {public static void main (String [] args) throws AxisFault {// RPCServiceClient is the RPC method that calls RPCServiceClient client = new RPCServiceClient (); Options options = client. getOptions (); // set URLString address = "http: // localhost: 8080/axis2/services/HelloWorldService"; EndpointReference epf = new EndpointReference (address); options. setTo (epf);/*** sets the method to be called. http://ws.apache.org/axis2is The namespace of Method * default (no package, if there is a package name * is the http://service.hoo.com package name upside down * sayHello is the method name */QName qname = new QName ("http://ws.apache.org/axis2", "sayHello "); // specify the calling method and passing parameter data, and set the return value type Object [] result = client. invokeBlocking (qname, new Object [] {"jack"}, new Class [] {String. class}); System. out. println (result [0]); qname = new QName ("http://ws.apache.org/axis2", "getAge"); result = client. invokeBlocking (qname, new Object [] {new Integer (22)}, new Class [] {int. class}); System. out. println (result [0]) ;}}

If you understand or have read axis1.x's WebService, the client call code here is relatively simple, and there are some key comments, so I will not go into details here. It is worth noting that the returned Object called by the axis2 WebService client is an array of objects, which is very different from axis1.x. We usually take the first value of the array and convert it into a forced conversion.

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.