Axis2: Creating and consuming a web service

Objective

  • Configuring axis2
  • Exposing a POJO as a web service using axis2
  • Consuming the developed web service

Configuring AXIS-2 with Tomcat

  • Download axis2 binary distribution.
  • Set the environment variable AXIS2_HOME. Add %AXIS2_HOME%\bin in path variable.
  • Go to axis2_home/webapp directory and run ant command. Make sure ant is configured on your machine. This will create axis2.war in the %AXIS2_HOME%/ext directory.
  • Deploy the axis2.war in tomcat.
  • A welcome page at the URL http://localhost:8080/axis2/ will confirm the successful deployment of axis2. You can browse through this welcome page to see what all is there.

Creating a webservice

  • Create a directory named GreetService.
  • Create the com/technicalmusings/samples/axis2/service/GreetService.java file.
      GreetService.java
      package com.technicalmusings.samples.axis2.service; 

       public class GreetService{
           public String getServerTime(){
                return (new java.util.Date()).toString();
           }
           public String greet(String name){
                return "Hello " + name ;
           }
       }
  • Compile the class.
    GreetService>javac -g -d . com\technicalmusings\samples\axis2\service\GreetService.java
    

*The –g option is needed while compiling the class as it adds the debug information to the class and using which the java2wsdl tool generates the name of the parameters (or variables). Without the –g option the parameter name may be generated as param0 or arg0, depending in the version of axis2.

  • Create the directory META-INF in GreetService directory.
  • Create the service.xml file in the META-INF directory.
      services.xml
    <?xml version="1.0" encoding="UTF-8"?> 

    <serviceGroup>
       <service name="GreetService">
          <description>First Axis Service: Greet Service</description>
          <parameter name="ServiceClass" locked="false">
               com.technicalmusings.samples.axis2.service.GreetService
          </parameter>
          <operation name="getServerTime">
                <messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-only"
                  class="org.apache.axis2.rpc.receivers.RPCInOnlyMessageReceiver" />
         </operation>
         <operation name="greet">
                <messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-out"
                  class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" />
        </operation>
     </service>
   </serviceGroup>
  • Use the java2wsdl tool supplied with the axis2 distribution to generate the wsdl file in the META-INF directory as below:
      GreetService>%AXIS2_HOME%\bin\java2wsdl -cp . -cn 
      com.technicalmusings.samples.axis2.service.GreetService
      -of META-INF\GreetService.wsdl 
  • Create an axis archive file using the following command:
      GreetService>jar -cvf GreetService.aar .
  • Copy GreetService.aar to the deployed axis2.war to <TOMCAT_HOME>\webapps\axis2\WEB-INF\services.
  • Start/Restart the tomcat server and check to ensure the proper deployment of the service by hitting the URL, http://localhost:8080/axis2/services/listServices.

Consuming the Webservice

  • Create a directory GreetServiceClient.
  • Use the wsdl2java tool to generate the java classes for the GreetService. Execute the following command.
      GreetServiceClient>%AXIS2_HOME%\bin\wsdl2java 
      -ss -g -uri http://localhost:8080/axis2/services/GreetService?wsdl 
  • On successful execution the tool will generate the artifacts in the GreetServiceClient folder along with an ant build script and the wsdl file in the resources folder.
    You can see other options for wsdl2java tool for different configurations.
  • Write a java class to test the web service. GreetServiceClient/src/com/technicalmusings/samples/client/GreetServiceClient.java.

GreetServiceClient.java

       package com.technicalmusings.samples.axis2.service;

       import com.technicalmusings.samples.axis2.service.GetServerTimeResponse;
       import com.technicalmusings.samples.axis2.service.Greet;
       import com.technicalmusings.samples.axis2.service.GreetResponse;
       import com.technicalmusings.samples.axis2.service.GreetServiceStub;

       public class GreetServiceClient {

          public static void main(String[] args) throws Exception{
             //Create the stub
             GreetServiceStub stub =
               new GreetServiceStub("http://localhost:8080/axis2/services/GreetService");

            //Call the getServerTime method
            GetServerTimeResponse timeResponse = stub.getServerTime();
            System.out.println("The server time is:" + timeResponse.get_return());

            //Call the greet method
            Greet greetInput = new Greet();
            greetInput.setName("Axis2");
            GreetResponse greetResponse = stub.greet(greetInput);
            System.out.println("Server responded:" + greetResponse.get_return());
        }

     }
  • Now in order to compile this class we need to set the required libraries in the classpath. I am using minimum libraries needed to run the axis2 client. The version of axis2 I am using is axis2-1.5.1.
    Add following jars from %AXIS2_HOME%\bin to the classpath for compiling the client.
            activation-1.1.jar,
            axiom-api-1.2.8.jar,
            axiom-dom-1.2.8.jar,
            axiom-impl-1.2.8.jar,
            axis2-adb-1.5.1.jar,
            axis2-kernel-1.5.1.jar,
            axis2-transport-http-1.5.1.jar,
            axis2-transport-local-1.5.1.jar,
            commons-codec-1.3.jar,
            commons-fileupload-1.2.jar,
            commons-httpclient-3.1.jar,
            commons-logging-1.1.1.jar,
            geronimo-stax-api_1.0_spec-1.0.1.jar,
            httpcore-4.0.jar,
            mail-1.4.jar,
            neethi-2.0.4.jar,
            woden-api-1.0M8.jar,
            woden-impl-dom-1.0M8.jar,
            wsdl4j-1.6.2.jar,
            wstx-asl-3.2.4.jar,
            XmlSchema-1.4.3.jar

Compile the classes:

\GreetServiceClient\src>javac -d . com\technicalmusings\samples\axis2\service\*.java

Run the class:

\GreetServiceClient\src>java com.technicalmusings.samples.axis2.service.GreetServiceClient

Output:

The server time is:Mon Feb 01 17:55:29 IST 2010
Server responded:Hello Axis2

Enjoy Learning,
Kamlesh

<!–[if gte mso 9]> Normal 0 false false false EN-IN X-NONE X-NONE <![endif]–><!–[if gte mso 9]> <![endif]–> <!–[endif]–>

Objective:

  • Configuring axis2
  • Exposing a POJO as a web service using axis2
  • Consuming the developed web service

Configuring AXIS-2 with Tomcat

  • Download axis2 binary distribution.
  • Set the environment variable AXIS2_HOME. Add %AXIS2_HOME%\bin in path variable.
  • Go to axis2_home/webapp directory and run ant command. Make sure ant is configured on your machine. This will create axis2.war in the axis2_home/ext directory.
  • Deploy the axis2.war in tomcat.
  • A welcome page at the URL http://localhost:8080/axis2/ will confirm the successful deployment of axis2. You can browse through this welcome page to see what all is there.

Creating a webservice

  • Create a directory named GreetService.
  • Create the com/technicalmusings/samples/axis2/service/GreetService.java file.

GreetService.java

package com.technicalmusings.samples.axis2.service;

public class GreetService{
public String getServerTime(){
return (new java.util.Date()).toString();
}
public String greet(String name){
return “Hello ” + name ;
}
}

  • Compile the class.

GreetService>javac -d . com\technicalmusings\samples\axis2\service\GreetService.java

<!–[if !supportLineBreakNewLine]–>
<!–[endif]–>

  • Create the directory META-INF in GreetService directory.
  • Create the service.xml file in the META-INF directory.

services.xml

<?xml version=”1.0″ encoding=”UTF-8″?>

<serviceGroup>
<service name=”GreetService”>
<description>First Axis Service: Greet Service</description>
<parameter name=”ServiceClass” locked=”false”>
com.technicalmusings.samples.axis2.service.GreetService
</parameter>
<operation name=”getServerTime”>
<messageReceiver mep=”http://www.w3.org/2004/08/wsdl/in-only
class=”org.apache.axis2.rpc.receivers.RPCMessageReceiver” />
</operation>
<operation name=”greet”>
<messageReceiver mep=”http://www.w3.org/2004/08/wsdl/in-out
class=”org.apache.axis2.rpc.receivers.RPCMessageReceiver” />
</operation>
</service>
</serviceGroup>

  • Use the java2wsdl tool supplied with the axis2 distribution to generate the wsdl file in the META-INF directory as below:

GreetService>%AXIS2_HOME%\bin\java2wsdl -cp . -cn

com.technicalmusings.samples.axis2.service.GreetService -of META-

INF\GreetService.wsdl

  • Create an axis archive file using the following command:

GreetService>jar -cvf GreetService.aar .

*Note the dot(.) in the command.
<!–[if !supportLineBreakNewLine]–>
<!–[endif]–>

  • Copy GreetService.aar to the deployed axis2.war which is <TOMCAT_HOME>\webapps\axis2\WEB-INF\services.
  • Start/Restart the tomcat server and check to ensure the proper deployment of the service by hitting the URL,

http://localhost:8080/axis2/services/listServices.

Consuming the Webservice

  • Create a directory GreetServiceClient.
  • Use the wsdl2java tool to generate the java classes for the GreetService. Execute the following command.

GreetServiceClient>%AXIS2_HOME%\bin\wsdl2java -ss -g -uri

http://localhost:8080/axis2/services/GreetService?wsdl

  • On successful execution the tool will generate the artifacts in the GreetServiceClient folder along with an ant build script and the wsdl file in the resources folder.

You can see other options for wsdl2java tool for different configurations.

  • Write a java class to test the web service. GreetServiceClient/src/com/technicalmusings/samples/service/GreetServiceClient.java.

GreetServiceClient.java

package com.technicalmusings.samples.axis2.service;

import com.technicalmusings.samples.axis2.service.GetServerTimeResponse;
import com.technicalmusings.samples.axis2.service.Greet;
import com.technicalmusings.samples.axis2.service.GreetResponse;
import com.technicalmusings.samples.axis2.service.GreetServiceStub;

public class GreetServiceClient {

public static void main(String[] args) throws Exception{
//Create the stub
GreetServiceStub stub =
new GreetServiceStub(“http://localhost:8080/axis2/services/GreetService“);

//Call the getServerTime method
GetServerTimeResponse timeResponse = stub.getServerTime();
System.out.println(“The server time is:” + timeResponse.get_return());

//Call the greet method
Greet greetInput = new Greet();
greetInput.setParam0(“Axis2”);
GreetResponse greetResponse = stub.greet(greetInput);
System.out.println(“Server responded:” + greetResponse.get_return());
}

}

  • Now in order to compile this class we need to set the required libraries in the classpath. I am using minimum libraries needed to run the axis2 client.

The version of axis2 I am using is axis2-1.5.1.

Set the classpath:

\GreetServiceClient\src\com\technicalmusings\samples\axis2\service>set classpath=%AXIS2_HOME%/lib/activation-1.1.jar;

%AXIS2_HOME%/lib/axiom-api-1.2.8.jar;

%AXIS2_HOME%/lib/axiom-dom-1.2.8.jar;

%AXIS2_HOME%/lib/axiom-impl-1.2.8.jar;

%AXIS2_HOME%/lib/axis2-adb-1.5.1.jar;

%AXIS2_HOME%/lib/axis2-kernel-1.5.1.jar;

%AXIS2_HOME%/lib/axis2-transport-http-1.5.1.jar;

%AXIS2_HOME%/lib/axis2-transport-local-1.5.1.jar;

%AXIS2_HOME%/lib/commons-codec-1.3.jar;

%AXIS2_HOME%/lib/commons-fileupload-1.2.jar;

%AXIS2_HOME%/lib/commons-httpclient-3.1.jar;

%AXIS2_HOME%/lib/commons-logging-1.1.1.jar;

%AXIS2_HOME%/lib/geronimo-stax-api_1.0_spec-1.0.1.jar;

%AXIS2_HOME%/lib/httpcore-4.0.jar;

%AXIS2_HOME%/lib/mail-1.4.jar;

%AXIS2_HOME%/lib/neethi-2.0.4.jar;

%AXIS2_HOME%/lib/woden-api-1.0M8.jar;

%AXIS2_HOME%/lib/woden-impl-dom-1.0M8.jar;

%AXIS2_HOME%/lib/wsdl4j-1.6.2.jar;

%AXIS2_HOME%/lib/wstx-asl-3.2.4.jar;

%AXIS2_HOME%/lib/XmlSchema-1.4.3.jar;%classpath%;.

Compile the classes:

\GreetServiceClient\src>javac -d . com\technicalmusings\samples\axis2\service\*.java

Run the class:

\GreetServiceClient\src>java com.technicalmusings.samples.axis2.service.GreetServiceClient

Output:

The server time is:Mon Feb 01 17:55:29 IST 2010
Server responded:Hello Axis2
<!–[if !supportLineBreakNewLine]–>
<!–[endif]–>

Enjoy Learning,
Kamlesh

 

Advertisements

9 thoughts on “Axis2: Creating and consuming a web service

  1. this page is very informative. The flow given is very neat and clear. Thanks kamlesh for sharing the information and please keep up the good work.

  2. The way you described the creation and consuming of webservices is very much helpful for a beginner like me.

    Good Work thanks alot..

    • Very clear Kamlesh, Good work.

      Any idea about resolving this exception please? I tried changing different versions of Axis2, but in vain. Any help is appreciated, Thanks.

      And the servicesList says “FaultyService”

      Error: org.apache.axis2.deployment.DeploymentException: com.ctc.wstx.exc.WstxParsingException: Illegal processing instruction target (“xml”); xml (case insensitive) is reserved by the specs. at [row,col {unknown-source}]: [1,6] at org.apache.axis2.deployment.repository.util.ArchiveReader.processServiceGroup(ArchiveReader.java:150)……

  3. java version “1.6.0_26”
    Java(TM) SE Runtime Environment (build 1.6.0_26-b03)

    Axis: 1.5.1, 1.5.2, 1.6.0, 1.6.2

    Tomcat 7

    Please let me know for more details.

    Thanks
    Raza

    • Hi,

      I don’t think Java 7 should be the problem. Please check if all the generated artifacts are on the classpath else please provide the error details here.

      – Kamlesh

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s