Java programmers from the stupid bird to the rookie (110) Step by Step learning WebService (iv) AXIS2 development WebService detailed

Source: Internet
Author: User
Tags web services wsdl

Axis2 is a third-party Web services engine provided by Apache, Axis2 more efficient, modular, and more XML-oriented than its predecessor, Apache axis.  After careful design, AXIS2 provides more convenient module add function. The new architecture is new and does not use Axis 1.x's common code. The motivation to support development Axis2 is to explore more modular, more flexible, and more efficient architectures that can easily be plugged into implementations of other relevant WEB service standards and protocols such as Ws-security, ws-reliablemessaging, and so on. The Apache Axis2 is a follow-on version of Axis and is a new generation of soap engines. Web Service is now the best technology for implementing SOA, and Axis2 is a relatively mature and popular technology Framework (architecture) for implementing Web service today. Let's take a look at the main process steps for AXIS2 development in WebService.

Before using, of course, you need to install AXIS2-related services, before installing the Axis2 service, of course, first download the relevant installation files. Download Address:


Before downloading the relevant files, let's start with a package about AXIS2:

For Axis2, the official offers four types of packages, binary packages, war packages, document packages, and source packages, respectively. The binary package contains sample programs and some jar packages, which are AXIS2 service packs that can be deployed as Web applications to servlet container, a tool-class Web application for service management provided by Apache Axis2. The so-called AXIS2 installation means that the package is installed in the servlet container, the document package is no longer said, and the source package is not said to be more.
The official documentation says the resources needed to use Axis2 are Java5 and above, hard disk space not less than 11M, and a random operating system. In addition, in order to package binary data into a war file, you need to install not less than 1.6.5 Apache ant, in order to build the source package, you need to install no less than maven2.0.7 maven.

OK, let's get together and install Axis2:

1. Download the Axis2 War package.

2. Unzip the downloaded war package to the WebApps under the Tomcat directory.

3. Start Tomcat. The Axis2 folder and related files are generated under the WebApps directory.

Access http://localhost:8080/axis2/can see the following page indicating that Axis2 is running successfully.

OK, installation is complete, simple.

After installing Axis2. Next we can use it to develop our WebService program.

1. Create a Web project first

2. Create service class HelloWorld

Package com.bzu.csh;
public class HelloWorld {public
string Gethello (string name) {return
"Hello," + name +. ";
Public String Getworld (string name) {return
' world, ' + name + '. ';
Public String Gethelloworld () {return
' Hello,world ';

3. Modify the Web.xml as follows:

<?xml version= "1.0" encoding= "UTF-8"?> <web-app version= "2.5" xmlns= ""
Java ee "
xmlns:xsi=" "
xsi:schemalocation=" Xml/ns/javaee ">
    <!--loading axis-->
< servlet>
<servlet-class> Org.apache.axis2.transport.http.axisservlet</servlet-class>
<load-on-startup>1</ load-on-startup>
<servlet-name>AxisServlet< /servlet-name>

4. Copy the modules, service and conf files under the Webapps/axis2/web-inf under the Tomcat installation directory to the Web-inf directory under HelloWorld. Under Lib, the following jar package is also copied to the past. Then under Services, create a new helloworld/meta-inf path, meta-inf under new Services.xml, as follows:

<service name= "HelloWorld" >  
        HelloWorld service Example
    <parameter name= "ServiceClass" >  
    <operation Name= "Gethello" >  
        <messagereceiver class= "Org.apache.axis2.rpc.receivers.RPCMessageReceiver"/>  
    <operation name= "Getworld" >  
        <messagereceiver class= " Org.apache.axis2.rpc.receivers.RPCMessageReceiver "/>  
    <operation name=" Gethelloworld ">  
<!--Here you should note that you do not use 
the return value Org.apache.axis2.rpc.receivers.RPCInOnlyMessageReceiver, no parameters or rpcmessagereceiver-->
        < Messagereceiver class= "Org.apache.axis2.rpc.receivers.RPCMessageReceiver"/>  

Here we have a look at the Service.xml configuration:

1.<service name= "HelloWorld" > Specify the service name here.

2. <description> Service Description

3. <parameter name= "ServiceClass" > Service-level parameters

In the Services.xml file, we can define parameters directly under the service node, which are accessed by the message context (at run time), Axisservice, or axisoperation. Parameter has a required and optional argument: The parameter name is a required argument. The service parameter here is the specified service class.

4. <operation name= "SayHello" >

Service-Level message sinks

The message sink in AXIS2 is a special processor and is the last processor in the in Path (request path). Each operation in a Web service has its own message sink, and different operations can have different message sinks. Message sinks are dependent on message Exchange Mode, so we must specify different message sinks for different message exchange modes.

How do you specify the same message sink for all operations? Just add a service-level message sink. So we don't have to specify the message sink at the operation level. What we're going to do is specify a service-level message sink. At deployment time, Axis2 automatically chooses the correct message receiver for the operation. Here we specify

Operation-Level message receiver

The preceding article describes how to specify a service-level message sink. However, we can also specify different message sinks for different operations, which requires the messagereceiver tag to be specified in the operation

Finally, a problem with writing a services.xml file for deploying a service group is described

Service groups are a convenient way to deploy multiple services in a single service pack file. Of course, there should be a logical relationship between these services. The only difference between a services.xml file used for a service group and a single service is the root element. For service groups, the root element is servicegroup, and we can define multiple service elements within the Servicegroup element.


<service name=service1>



<service name=service2>




Start Tomcat and then access

Can see the service information.

Below we can write a client to invoke the service program we wrote.

Package com.bzu.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 Clienttest {public static void main (string[] args) {String url = '
HelloWorld ";
String result = null;
try {//Use RPC method to invoke WebService rpcserviceclient serviceclient = new Rpcserviceclient ();
Options options = Serviceclient.getoptions ();
Specifies the URL to invoke WebService endpointreference Targetepr = new EndpointReference (URL);
Options.setto (TARGETEPR); When you create a QName object, the first parameter of the QName class's constructor represents the namespace name of the WSDL file, which is the targetnamespace property value of the <wsdl:definitions> element///
Specifies the namespace of the Getworld method to invoke and the WSDL file ...
QName opaddentry = new QName ("", "Getworld");
Specifies the parameter value of the GetGreeting method, if there are more than one, to continue to add to the back, without specifying the name of the parameter object[] Opaddentryargs = new object[] {"Java"}; Returns the parameter type, which is somewhat different from the AXIS1//Invokeblocking method has three parameters, where the type of the first parameter is a QName object representing the method name to invoke;//The second parameter represents the webs to invokeThe parameter value of the Ervice method, the parameter type is object[];//The third parameter represents the class object of the return value type of the WebService method, and the parameter type is class[]. When a method has no parameters, the second parameter value of the Invokeblocking method cannot be null, but to use the new object[]{}//If the invoked WebService method does not return a value, You should use the Invokerobust method of the Rpcserviceclient class,//The method has only two parameters, and they have the same meaning as the first two parameters of the Invokeblocking method.
Class object that specifies the data type of the GetGreeting method return value ...
Class[] classes = new class[] {string.class}; Call the GetGreeting method and output The return value of the method .... result = (String) serviceclient.invokeblocking (Opaddentry, Opaddentryargs,
classes) [0];
Here's the code that calls the Gethello method, similar to the code that calls the Getworld method classes = new class[] {string.class};
Opaddentry = new QName ("", "Gethello");
Opaddentryargs = new object[] {"Cao Shenhuan"};
System.out.println (Serviceclient.invokeblocking (Opaddentry, Opaddentryargs, classes) [0]);
The following is the code that invokes the Gethelloworld method opaddentry = new QName ("", "Gethelloworld");
System.out.println (Serviceclient.invokeblocking (Opaddentry, New object[]{}, classes) [0]);
catch (Exception e) {e.printstacktrace ();}}

Run Result:

All kinds of detailed application are explained in the annotation. I don't think I need to say it again. Note that parameters do not configure errors

Recommended excellent axis2 to explain the blog:


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.