Use pojo to implement 0-configured webservice

Source: Internet
Author: User
Tags wsdl
Axis2 is a brand new WebService engine. This version is the product of the redesign of Axis1.x. Axis2 not only supports SOAP1.1 and SOAP1.2, but also integrates the popular REST WebService and Spring and JSON technologies. These will be explained in the subsequent series of tutorials. This article describes how to use Axis2 to develop a WebService without any configuration files, and call this WebService using Java and C # on the client.

1. Download and install Axis2

You can download the latest version of Axis2 from the following URL:


The latest version of Axis2 1.4.1 is used in this article. You can download the following two zip packages:

The file contains all the jar files in Axis2, which are used to publish WebService to a Web container.

Extract the file to the corresponding directory, and put the axis2.war file in the directory In the \ webapps directory (the Tomcat version used in this article is 6.x), and start Tomcat.

Enter the following URL in the address bar of the browser:

Http: /localhost: 8080/axis2/

If the page shown in 1 is displayed in the browser, Axis2 is successfully installed.

Figure 1
2. compile and publish WebService

The impression of a service program implemented in Java is that a large amount of configuration is required, but this will be terminated in axis2. In Axis2, a simple POJO can be directly published as a WebService without any configuration. All the public methods in POJO will be published as WebService methods.

The following code implements a simple POJO:

Public class SimpleService
Public String getGreeting (String name)
Return "hello" + name;
Public int getPrice ()
Return new java. util. Random (). nextInt (1000 );
There are two methods in the SimpleService class. Since both methods are public methods, both of them will be published as WebService methods.

After the SimpleService class is compiled, put the SimpleService. class file \ Webapps \ axis2 \ WEB-INF \ pojo directory (created if there is no pojo directory ). Now we have successfully published the SimpleService class as WebService. Enter the following URL in the address bar of the browser:

Http: /localhost: 8080/axis2/services/listServices

The current page displays all WebServices published in Axis2, as shown in figure 2.

Figure 2
Enter the following two URLs in the address bar of the browser to test the getGreeting and getPrice methods respectively:

Http: // localhost: 8080/axis2/services/SimpleService/getGreeting? Name = bill

Http: /localhost: 8080/axis2/services/SimpleService/getPrice

Figure 3 and figure 4 show the test results of the getGreeting and getPrice methods.

Figure 3 test results of the getGreeting Method

Figure 4 test results of the getPrice Method

Pay attention to the following points when writing, releasing, and testing a WebService with 0 configurations:

1. The POJO class cannot use the package keyword to declare a package.

2. Axis2 can hot publish WebService by default. That is to say, Tomcat can automatically publish WebService without restarting when the. class file of WebService is copied to the pojo directory. If you want to cancel the hot release function of Axis2, you can enable \ Webapps \ axis2 \ WEB-INF \ conf \ axis2.xml, find the following configuration code:

True Change "true" to "false. Note that Axis2 is a hot release by default, but it is not a hot update. That is to say, once a WebService is successfully released and you want to update the WebService, you must restart Tomcat. This is inconvenient for developers to debug WebService. Therefore, Axis2 can be set to hot update when developing WebService. In the axis2.xml file, find False , Change "false" to "true.

3. when testing WebService in a browser, if the WebService method has parameters, you need to use the URL request parameters to specify the value of the WebService method parameter. The request parameter name must be consistent with the method parameter name. For example, to test the getGreeting method, the request parameter name must be name, as shown in the preceding URL.

4. The pojo directory for WebService publishing is the default one. If you want to publish WebService in another directory, open the axis2.xml file and add the following sub-elements to the element:

The above configuration allows Publish WebService in the \ webapps \ axis2 \ WEB-INF \ my directory. For example, copying SimpleService. class in this example to the my directory can also be published successfully (but you need to delete SimpleService. class in the pojo directory, otherwise WebService will be renamed ).

3. Use Java to call WebService client programs

WebService is a program service. It is meaningless to access WebService only in a browser. Therefore, Java is used in this section to implement a console program to call the WebService published in the previous section. The client code that calls WebService is as follows:

Package 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 RPCClient
Public static void main (String [] args) throws Exception
// Call WebService using RPC
RPCServiceClient serviceClient = new RPCServiceClient ();
Options options = serviceClient. getOptions ();
// Specify the URL to call WebService
EndpointReference targetEPR = new EndpointReference (
"Http: // localhost: 8080/axis2/services/SimpleService ");
Options. setTo (targetEPR );
// Specify the parameter value of the getGreeting Method
Object [] opaddentryargs = new object [] {"Superman "};
// Specifies the Class Object of the Data Type returned by the getgreeting Method
Class [] classes = new class [] {string. Class };
// Specify the getgreeting method to call and the namespace of the WSDL File
QNAME opaddentry = new QNAME ("", "getgreeting ");
// Call the getgreeting method and output the return value of the Method
System. Out. println (serviceclient. invokeblocking (opaddentry, opaddentryargs, classes) [0]);
// The following code calls the getprice method. The code is similar to the code that calls the getgreeting method.
Classes = new class [] {Int. Class };
Opaddentry = new QNAME ("", "getprice ");
System. Out. println (serviceclient. invokeblocking (opaddentry, new object [] {}, classes) [0]);
After running the above program, the following information will be output on the console:

Hello Superman
Note the following when writing client code:

1. The client code must reference many jar packages of Axis2. If you are not sure which jar package to reference, you can reference all jar packages in the lib directory of the Axis2 release package in Eclipse projects.

2. In this example, the invokeBlocking method of the RPCServiceClient class is used to call the method in WebService. The invokeBlocking method has three parameters. The first parameter is a QName object, indicating the name of the method to be called. The second parameter indicates the parameter value of the WebService method to be called, 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 []. If the method has no parameters, the second parameter value of the invokeBlocking method cannot be null, but new Object [] {} must be used.

3. If the called WebService method does not return a value, use the invokeRobust method of the RPCServiceClient class. This method has only two parameters, and their meanings are the same as those of the first two parameters of the invokeBlocking method.

4. 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 The targetNamespace attribute value of the element. The following is the code snippet of the WSDL file generated by the SimpleService class:

4. Use wsdl2java to simplify client writing

Many readers may say, "Is there a mistake? It is too troublesome to write so much code to call only two WebService methods ".

Fortunately, Axis2 provides a wsdl2java. bat command to automatically generate WebService calling code based on the WSDL file. The wsdl2java. bat command can be found in the "bin directory. You need to set the AXIS2_HOME environment variable before using the wsdl2java. bat command. The variable value is.

On the Windows console, output the following command line to generate the WebService call code:

% AXIS2_HOME % \ bin \ wsdl2java-uri http: // localhost: 8080/axis2/services/SimpleService? Wsdl-p client-s-o stub

The-url parameter specifies the path of the wsdl file, either a local path or a network path. The-p parameter specifies the package name of the generated Java class, and the-o parameter specifies the root directory for saving a series of generated files. After executing the above command, the reader will find that there is an additional stub directory in the current directory. you can find a SimpleServiceStub in the "stub" src "client directory. java file, which calls WebService in a complex way. You can directly use this class in your program. The Code is as follows:

Package 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 StubClient
Public static void main (String [] args) throws Exception
SimpleServiceStub stub = new SimpleServiceStub ();
SimpleServiceStub. GetGreeting gg = new SimpleServiceStub. GetGreeting ();
Gg. setName ("bill ");
System. out. println (stub. getGreeting (gg). get_return ());
System. out. println (stub. getPrice (). get_return ());
The preceding Code greatly simplifies WebService calling and simplifies the code. Note that wsdl2java. the Stub class generated by the bat command encapsulates the parameters of the WebService method in the corresponding class, and the class name is the method name. For example, the parameters of the getGreeting method are encapsulated in the GetGreeting class, to call the getGreeting method, you must first create an object instance of the GetGreeting class.

5. Use C # To Call WebService

Theoretically, WebService can be called by any language that supports the SOAP protocol. Using C # To Call WebService in Visual Studio is the easiest way to implement WebService in all languages (similar methods for calling are also very simple ).

Create a Visual Studio project, enter the following URL in the reference Web service dialog box, and enter the Web reference name "WebService ":

Http: // localhost: 8080/axis2/services/SimpleService? Wsdl

Then, the reference Web service dialog box displays all the methods in the WebService, as shown in Figure 5.

Figure 5
After completing the above work, you only need the following three lines of C # code to call the getGreeting and getPrice methods and display the return values of these two methods:

WebService. SimpleService simpleService = new WSC. WebService. SimpleService ();
MessageBox. Show (simpleService. getGreeting ("bill "));
MessageBox. Show (simpleService. getPrice (). @ return. ToString ());
When. net parses the WSDL file, it directly maps the parameter of the getGreeting Method to the String type. Therefore, you can directly pass the value.
From the above call process, we can see that the process of adding a Web reference is equivalent to the process of Automatically Generating stub classes by calling wsdl2java. bat in Java. It is only the difference between calling stub class and C # class, but in general, it greatly simplifies the WebService call process.

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: 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.