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 http://ws.apache.org/axis2/axis2: two zip packages can be downloaded: The axis2-1.5.4-bin.zip axis2-1.5.4-war.zip contains all jar files in Axis2, The axis2-1.5.4-bin.zip file is used
1. Download and install Axis2
1. You can download the latest version of Axis2 from the http://ws.apache.org/axis2:
You can download the following two zip packages:
Axis2-1.5.4-bin.zip
Axis2-1.5.4-war.zip
The axis2-1.5.4-bin.zip file contains all the jar files in Axis2,
The axis2-1.5.4-war.zip file is used to publish a WebService to a Web container.
2.decompress the axis2-1.5.4-war.zip 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
- Public class HelloService {
- Public String sayHello (){
- Return "hello ";
- }
- Public String sayHelloToPerson (String name ){
- If (name = null ){
- Name = "nobody ";
- }
- Return "hello," + name;
- }
- }
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
-
- 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
- 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
- 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
- Package com. sinosoft. webservice;
- Public class HelloServiceNew {
- Public String sayHelloNew (){
- Return "hello ";
- }
- Public String sayHelloToPersonNew (String name ){
- If (name = null ){
- Name = "nobody ";
- }
- Return "hello," + name;
- }
- Public void updateData (String data ){
- System. out. println (data + "updated. ");
- }
- }
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
-
-
-
- Web Service example
-
-
- Com. sinosoft. webservice. HelloServiceNew
-
-
-
- Class = "org. apache. axis2.rpc. receivers. RPCMessageReceiver"/>
-
- 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
-
-
- Web Service example
-
-
- Com. sinosoft. webservice. HelloServiceNew
-
-
-
-
-
-
- Class = "org. apache. axis2.rpc. receivers. RPCInOnlyMessageReceiver"/>
-
-
To publish multiple WebServices, you can use Element
Xml Code
-
-
- ...
-
-
- ...
-
-
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
- 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 TestMain {
- Public static void main (String args []) throws AxisFault {
- // 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/HelloService ");
- Options. setTo (targetEPR );
- // Specify the parameter value of the sayHelloToPerson Method
- Object [] opAddEntryArgs = new Object [] {"beauty "};
- // Specifies the Class Object of the Data Type returned by the sayHelloToPerson Method
- Class [] classes = new Class [] {String. class };
- // Specify the sayHelloToPerson method to call and the namespace of the WSDL File
- QName opAddEntry = new QName ("http://ws.apache.org/axis2", "sayHelloToPerson ");
- // Call the sayHelloToPerson method and output the return value of this method
- System. out. println (serviceClient. invokeBlocking (opAddEntry, opAddEntryArgs, classes) [0]);
- }
- }
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
- Package client;
- Public class StupTest {
- Public static void main (String [] args) throws Exception
- {
- HelloServiceStub stub = new HelloServiceStub ();
- HelloServiceStub. SayHelloToPerson gg = new HelloServiceStub. SayHelloToPerson ();
- Gg. setName ("beauty ");
- System. out. println (stub. sayHello (). get_return ());
- System. out. println (stub. sayHelloToPerson (gg). get_return ());
- }
- }
The output result is as follows:
Hello
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.