Create Axis2 client project with Axis2M

Introduction

Axis2M simplify axis2 web service development workload with the help of Maven build tool .Also axis2m promote usage of standard j2ee web application project layout for axis2 development this will enable lot of facilities that available for normal j2ee development to use through maven such as WAR packaging , embedded Jetty etc.

Basically axis2M provide following two artifacts to simplify axis2 development.

  1. Axis2M quick start archetype - This facilitates to create and deploy your first Axis2 web service using a single maven command. This will rapidly reduce the learning curve for your first Axis2 web service experience and also you can use this as template for any of your axis2 project.
  2. Axis2 Maven plug in - Provide set of tools to simplify development of axis2 services and modules .some of these goals are customized versions of original goals provided by Axis2. Easy integration with spring framework is one of the top level objectives of this.

Create your first project

Step -1 It is assumed that you have installed Maven2 and configured for your environment. Open a console window and run following command.

mvn archetype:generate -DarchetypeCatalog=http://axis2m.sourceforge.net/repo/

This will show you a wizard to select the number corresponding to "axis2m quickstart" as shown in below.

D:\java\IDE\eclipse\MyWorks\axis2m>mvn archetype:generate -DarchetypeCatalog=http://axis2m.sourceforge.net/repo/
[INFO] Scanning for projects...
[INFO] Searching repository for plugin with prefix: 'archetype'.
[INFO] ------------------------------------------------------------------------
[INFO] Building Maven Default Project
[INFO]    task-segment: [archetype:generate] (aggregator-style)
[INFO] ------------------------------------------------------------------------
[INFO] Preparing archetype:generate
[INFO] No goals needed for project - skipping
[INFO] Setting property: classpath.resource.loader.class => 'org.codehaus.plexus.velocity.ContextClassLoaderResourceLoader'.
[INFO] Setting property: velocimacro.messages.on => 'false'.
[INFO] Setting property: resource.loader => 'classpath'.
[INFO] Setting property: resource.manager.logwhenfound => 'false'.
[INFO] [archetype:generate]
[INFO] Generating project in Interactive mode
[INFO] No archetype defined. Using maven-archetype-quickstart (org.apache.maven.archetypes:maven-archetype-quickstart:1.0)
Choose archetype:
1: http://axis2m.sourceforge.net/repo/ -> quickstart (axis2M archetype for creating a web Service application with AXIS2)
Choose a number:  (1):

[Note]Note:

if you get any error message during this phase please refer "installation and troubleshooting "to overcome the problem.

Provide following values for wizard

Choose a number:  (1):   1 
 Define value for groupId: : sample
Define value for artifactId: : quickstart
Define value for version:  1.0-SNAPSHOT: : 1.0

Press enter key for other inputs to take default values.

If you follow all these steps you can see the success message as below

Choose archetype:
1: http://axis2m.sourceforge.net/repo/ -> quickstart (axis2M archetype for creating a web Service application with AXIS2)
Choose a number:  (1): 1
Define value for groupId: : sample
Define value for artifactId: : quickstart
Define value for version:  1.0-SNAPSHOT: : 1.0
Define value for package:  sample: :
Confirm properties configuration:
groupId: sample
artifactId: quickstart
version: 1.0
package: sample
 Y: :
[INFO] ----------------------------------------------------------------------------
[INFO] Using following parameters for creating OldArchetype: quickstart:1.0.0
[INFO] ----------------------------------------------------------------------------
[INFO] Parameter: groupId, Value: sample
[INFO] Parameter: packageName, Value: sample
[INFO] Parameter: basedir, Value: D:\java\IDE\eclipse\MyWorks\axis2m
[INFO] Parameter: package, Value: sample
[INFO] Parameter: version, Value: 1.0
[INFO] Parameter: artifactId, Value: quickstart
[INFO] ********************* End of debug info from resources from generated POM ***********************
[INFO] OldArchetype created in dir: D:\java\IDE\eclipse\MyWorks\axis2m\quickstart
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------

Step -2

Now you can import newly created maven project in to your favirioute IDE, in eclipse you can see a new project is created with maven j2ee web application directory structure as below.

defult directory

In src/main/java/ simpleserver directory Java you can see a single class called .java this is your POJO class that will be exposed as your first web services.

The second important thing is the service.xml file; you can see service.xml (in) src/main /web-main/MET-INF /servces / directory. The exact reason to use this path is, this will enable you to deploy your service as either Axis2 AAR format or WAR java web app package.

Step 3

Now you have your first web services project, you can deploy it either of the following ways

  1. Deploy on embedded Jetty server. If you are familiar with java MVC frameworks like Tapestry, Structs2 or Wicket, you are already familiar with jetty server. In same console you can run jetty server as follows
    mvn jetty:run
    

    Now open a browser window and type following url to see WSDL definition for your first service.

    http://localhost:8080/axis2/services/SimpleService?wsdl
    
  2. Axis2M run goal. Axis2m run goal invoke AXIS2 Simple HTTP server within the Maven lifecycle, if your are much more familiar with Axis2 this is good option for you. In console run the following command
    mvn  axis2m:run 
    

    Now open window and as usually type the following URL and you can see the default screen of simple HTTp server with the service you just created.

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

Testing your first service

Again you can follow one of following method to test your service.

  1. Test with REST client. In this case you don't need any client code, use your browse as REST client, our first simple service has a operation called "add" that will take two integer arguments

    test with rest

  2. Test with generated client using WSDL2JAVA You can use a tool a called WSDL2Java provide by Axis2 to generate client stub using WSDL, to do that you have many choices
    1. Use Axis2 Maven WSDJAVA goal , but by default out put folder structure not compatible with Maven project layout so you need to manually move your generated source code in to src folder.
    2. Use your IDE like eclipse to generate your client code.
    3. Use Axis2M WSDL2JAVA goal - this is a customized version of axis2 wsdl 2 java goal so that generated stub code will lie in maven src directory and by- default this will be pre configured to receive WSDL definition for our quick start web service.

    You can create a new Maven project for client either using your IDE or arctype;genarte plug in or you can reuse the same project you crated . To generate stub code run the following maven command mvn axis2m:wsdl2java

    Here you don't want to specify -uri for WSdl because this is preconfigured to pick up url of our quick start service. For other cases you need to tell your service the wsdl URL explicitly to generate stub.

    D:\java\IDE\eclipse\MyWorks\axis2m\quickstart>mvn axis2m:wsdl2java
    D:\java\IDE\eclipse\MyWorks\axis2m\quickstart>set MAVEN_OPTS=-Xmx512m
    [INFO] Scanning for projects...
    [INFO] Searching repository for plugin with prefix: 'axis2m'.
    [INFO] org.apache.maven.plugins: checking for updates from axis2m
    [INFO] ------------------------------------------------------------------------
    [INFO] Building A custom project
    [INFO]    task-segment: [axis2m:wsdl2java]
    [INFO] ------------------------------------------------------------------------
    Downloading: http://repo1.maven.org/maven2/woodstox/wstx-asl/3.0.1/wstx-asl-3.0.1.pom
    [INFO] [axis2m:wsdl2java]
    Retrieving document at 'http://localhost:8080/axis2/services/SimpleService?wsdl'.
    [INFO] ------------------------------------------------------------------------
    [INFO] BUILD SUCCESSFUL
    [INFO] ------------------------------------------------------------------------
    
    

    Now create the following class called Client.

    public class Client {
    
    	public static void main(String[] args) {
    
    		try {
    			SimpleServiceStub stub = new SimpleServiceStub();
    			HelloService req = new HelloService();
    			req.setMsg(" AxIS2 users");
    			HelloServiceResponse res = stub.helloService(req);
    			System.out.println(res.get_return());
    
    		} catch (AxisFault e) {
    			e.printStackTrace();
    		} catch (RemoteException e) {
    			e.printStackTrace();
    		}
    
    	}
    
    }
    
    

    Now you can run this class on your IDE and see the result of you first web severe invocation.

    log4j:WARN No appenders could be found for logger (org.apache.axis2.description.AxisService).
    log4j:WARN Please initialize the log4j system properly.
    Hello  AxIS2 users
    

Packaging your first service.

You already deploy your service and tested with client too, but you may need to deploy it on a application server. Again you have two options

  1. package as AAR. AAR is specially designed archive format for axis 2 web service , our project POM is configured for axis2 AAR plug in so you can use following command to pancake you service as AAR.
    mvn  axis2:aar 
    

    In the target directory you can see simplService.aar package.

  2. 2. package as WAR. with Axis2m you can package your service as a WAR package using default "package " command of Maven , just run following command
    mvn package 
    

    You can see simplesErce.war in target directory. You can deploy this simple.war in any java web application server just like you deploy any other web applications.