Axis2 creates a webService instance

Source: Internet
Author: User
1. Download and install Axis2 1. You can download the latest version of Axis2 from ws. apache. orgaxis2: you can download the following two zip packages:

1. Axis2 download and installation 1. axis2's latest release can be downloaded from two zip packages can be downloaded: The contains all jar files in Axis2, The file is used

1. Download and install Axis2

1. You can download the latest version of Axis2 from the
You can download the following two zip packages:
The file contains all the jar files in Axis2,
The file is used to publish a WebService to a Web container.

2.decompress the file to the corresponding directory and put the axis2.war file in the directory \ Webapps directory,
Start Tomcat and enter the following URL in the address bar of the browser:
Http: // localhost: 8080/axis2/. If you see the axis2 homepage, the installation is successful.

2. compile and publish WebService

(1) publish in POJO format (no configuration required)

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 sample code is as follows:

Java code

  1. Public class HelloService {
  2. Public String sayHello (){
  3. Return "hello ";
  4. }
  5. Public String sayHelloToPerson (String name ){
  6. If (name = null ){
  7. Name = "nobody ";
  8. }
  9. Return "hello," + name;
  10. }
  11. }

Compile the HelloService class and put the HelloService. class file \ Webapps \ axis2 \ WEB-INF \ PojoDirectory
(If there is no pojo directory, this directory is created ). Now we have successfully published the HelloService class to WebService.
Enter the following URL in the address bar of the browser:
Http: /localhost: 8080/axis2/services/listServices

Enter the following two URLs in the address bar of the browser to test the sayHelloToPerson and sayHello methods respectively:
1. http: /localhost: 8080/axis2/services/HelloService/sayHello
2. http: // localhost: 8080/axis2/services/HelloService/sayHelloToPerson? Name = bill

The following result is displayed:

Xml Code

  1. Hello, bill

Pay attention to the following points when writing, publishing, and testing WebService:
1. The POJO class cannot use the package keyword to declare a package.

2. Axis2 can hot publish WebService by default, that is, when the. class file of WebService is copied to the pojo directory,
Tomcat can automatically publish WebService without restarting it.
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:

Xml Code

  1. True

Change "true" to "false. Note that Axis2 is a hot release by default, but not a hot update.
That is to say, once the 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

Xml Code

  1. False

Set false to true.

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

4. The pojo directory for publishing WebService is only default. If you want to publish WebService in other directories,
You can open the axis2.xml file and add the following child elements to the element:

Xml Code

The above configuration allows Publish WebService in the \ webapps \ axis2 \ WEB-INF \ my directory.
For example, you can copy HelloService. class in this example to the my directory and publish it successfully.
(Delete SimpleService. class in the pojo directory, otherwise WebService will be renamed again ).

(2) Use the services. xml configuration file for publishing

Using Axis2 to implement Web Service, although the POJO class can be directly published into the axis2 \ WEB-INF \ pojo directory,
No configuration is required, but these POJO classes cannot be in any package. This seems inconvenient.
Therefore, Axis2 allows publishing POJO classes with packages to Web Services. First, implement a POJO class. The Code is as follows:

Java code

  1. Package com. sinosoft. webservice;
  2. Public class HelloServiceNew {
  3. Public String sayHelloNew (){
  4. Return "hello ";
  5. }
  6. Public String sayHelloToPersonNew (String name ){
  7. If (name = null ){
  8. Name = "nobody ";
  9. }
  10. Return "hello," + name;
  11. }
  12. Public void updateData (String data ){
  13. System. out. println (data + "updated. ");
  14. }
  15. }

To publish the HelloServiceNew class as a Web Service, you need a Service. xml file,
This file needs to be placed in the META-INF directory with the following content:

Xml Code

  1. Web Service example
  2. Com. sinosoft. webservice. HelloServiceNew
  3. Class = "org. apache. axis2.rpc. receivers. RPCMessageReceiver"/>
  4. Class = "org. apache. axis2.rpc. receivers. RPCInOnlyMessageReceiver"/>

Where Used to publish a Web Service. Only one WebService class can be published for an element,
The name attribute indicates the WebService name. the following URL can obtain the WSDL content of the WebService:
Http: // localhost: 8080/axis2/services/HelloServiceNew? Wsdl
The name attribute name is "?" In the above URL "? "And.
Element indicates the description of the current Web Service, Element is used to set WebService parameters,
Set the class name for WebService.
It is worth noting that This element is used to set the processor for processing WebService methods.
For example, the sayHelloNew method has a return value. Therefore, you must use the RPCMessageReceiver class that can process input and output,
The updateData method does not return values. Therefore, you must use the RPCInOnlyMessageReceiver class that can only process the input.

To publish a WebService in this way, you must package it into a. aar file. The. aar file actually changes the. jar file extension.
Now we have created two files: HelloServiceNew. java and services. xml.
Compile HelloServiceNew. java to generate HelloServiceNew. class.
The location of services. xml and HelloServiceNew. class files is as follows:
D: \ ws \ com \ sinosoft \ webservice \ HelloServiceNew. class
D: \ ws \ META-INF \ services. xml
Go to the ws directory in the windows console and enter the following command to generate the. aar file.

Jar cvf ws. aar.

In fact, the. jar file can also publish webservice, but it is recommended that you use the. aar file to publish webservice in the official axis2 documentation.
Finally, copy the ws. aar file \ Webapps \ axis2 \ WEB-INF \ services Directory,
After starting Tomcat, you can call this WebService.

In addition, you can directly specify WebService methods in the services. xml file. For example, you can use the following configuration code to publish WebService

Xml Code

  1. Web Service example
  2. Com. sinosoft. webservice. HelloServiceNew
  3. Class = "org. apache. axis2.rpc. receivers. RPCInOnlyMessageReceiver"/>

To publish multiple WebServices, you can use Element

Xml Code

  1. ...
  2. ...

The Code omitted in the middle is the same as the configuration in the services. xml file.

3. Use Java to call WebService client programs

WebService is a program service. It is meaningless to access WebService only in a browser. The client code that calls WebService is as follows:

Java code

  1. Import javax. xml. namespace. QName;
  2. Import org. apache. axis2.AxisFault;
  3. Import org. apache. axis2.addressing. EndpointReference;
  4. Import org. apache. axis2.client. Options;
  5. Import org. apache. axis2.rpc. client. RPCServiceClient;
  6. Public class TestMain {
  7. Public static void main (String args []) throws AxisFault {
  8. // Call WebService using RPC
  9. RPCServiceClient serviceClient = new RPCServiceClient ();
  10. Options options = serviceClient. getOptions ();
  11. // Specify the URL to call WebService
  12. EndpointReference targetEPR = new EndpointReference (
  13. "Http: // localhost: 8080/axis2/services/HelloService ");
  14. Options. setTo (targetEPR );
  15. // Specify the parameter value of the sayHelloToPerson Method
  16. Object [] opAddEntryArgs = new Object [] {"beauty "};
  17. // Specifies the Class Object of the Data Type returned by the sayHelloToPerson Method
  18. Class [] classes = new Class [] {String. class };
  19. // Specify the sayHelloToPerson method to call and the namespace of the WSDL File
  20. QName opAddEntry = new QName ("", "sayHelloToPerson ");
  21. // Call the sayHelloToPerson method and output the return value of this method
  22. System. out. println (serviceClient. invokeBlocking (opAddEntry, opAddEntryArgs, classes) [0]);
  23. }
  24. }

Output result:
Hello, beauty

Note the following when writing client code:

1. The client code must reference many Axis2 jar packages. If the reader is not clear about which jar package to reference,
You can reference all jar packages in the lib directory of the Axis2 release package in Eclipse project.

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 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, which have the same meaning 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.

4. Use wsdl2java to simplify client writing

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/HelloService? 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 preceding command, you will find that there is an additional stub directory under the current directory,
You can find a HelloServiceStub. java file in the stub/src/client directory,
This file is complex to call WebService. You can use this class directly in the program. The Code is as follows:

Java code

  1. Package client;
  2. Public class StupTest {
  3. Public static void main (String [] args) throws Exception
  4. {
  5. HelloServiceStub stub = new HelloServiceStub ();
  6. HelloServiceStub. SayHelloToPerson gg = new HelloServiceStub. SayHelloToPerson ();
  7. Gg. setName ("beauty ");
  8. System. out. println (stub. sayHello (). get_return ());
  9. System. out. println (stub. sayHelloToPerson (gg). get_return ());
  10. }
  11. }

The output result is as follows:
Hello, beauty

The preceding Code greatly simplifies WebService calling and simplifies the code.
Note that the Stub class generated by the wsdl2java. bat command encapsulates the parameters of the WebService method in the corresponding class,
Class Name: method name. For example, parameters of the sayHelloToPerson method are encapsulated in the SayHelloToPerson class,
To call the sayHelloToPerson method, you must first create an object instance of the SayHelloToPerson class.

Related Article

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.