Search blog

Saturday, July 20, 2013

Embedding Apache Axis2 webservices into Existing WebApplications using Maven- SimpleWebservice

Let us see how to create & deploy Webservices using Axis2 and integrate Axis2 into another web application with Maven and how to deploy the application in j2ee server and how to access the webservices.



Developing the Webservice: [eclipse with Maven plugin and tomcat]
Open eclipse and Create a new maven project with war as outcome of the project. Follow the below steps to create a maven web project in eclipse.





Click on Finish to create the project.
The project will be created as below


Now we need to setup the axis2 support to the project to create a webservice/ expose the given java class to the public over the internet and allow the access using the webservice using axis2.

Extract the axis2-web.rar and copy the axis2-web folder into webapp folder.
Click here to get axis2-web

Extract the modules.rar and copy the modules folder into webapp/WEB-INF folder.
Get modules.rar form axis2-web



Create a folder with name services under WEB-INF folder and add the services.list and version-1.4.1.aar files into it.  (Get from axis2-web)

Now the Axis2 configuration is done for the project, this is one time activity to make a java class as a webservice.

Create a Java Class to expose as Webservice.


Give Name as MyFirstWebService and click on Finish.
Create a public method to expose and provide access through webservice.
Create a method with name sayHello which accepts the sting message and returns the message appended with “Hello ”.


Create a folder with the Name of the Class MyFirstWebService under WEB-INF/services and create folder META-INF under MyFirstWebService folder.

Create an xml file with the name of services.xml under WEB-INF/services/MyFirstWebService/META-INF folder.
Edit the services.xml with the following content and save. This is to indicate the axis2 to create a webservice. Axis2 will publish the given service class as a webservice.

xml version="1.0" encoding="UTF-8"?>
<serviceGroup>
                <service name="MyFirstWebService" scope="application">
                                <description>MyFirstWebService Sample </description>
   
                                <messageReceivers>
                                                <messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-only"
                                                                class="org.apache.axis2.rpc.receivers.RPCInOnlyMessageReceiver" />
                                                <messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-out"
                                                                class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" />
                                </messageReceivers>
                                <parameter name="ServiceClass">com.samplews.sampleWebService.MyFirstWebService</parameter>
                               
                                <operation name="sayHello">
                                                <messageReceiver  class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" />
                                </operation>

                </service>
</serviceGroup>


Create web.xml under WEB-INF folder.

Configure AxisServlet  and Axis2AdminServlet in web.xml.
xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
                xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
                id="WebApp_ID" version="2.5">
                <servlet>
                                <display-name>Apache-Axis Servlet</display-name>
                                <servlet-name>AxisServlet</servlet-name>
                                <servlet-class>org.apache.axis2.transport.http.AxisServlet</servlet-class>
                </servlet>
                <servlet-mapping>
                                <servlet-name>AxisServlet</servlet-name>
                                <url-pattern>/servlet/AxisServlet</url-pattern>
                </servlet-mapping>
                <servlet-mapping>
                                <servlet-name>AxisServlet</servlet-name>
                                <url-pattern>*.jws</url-pattern>
                </servlet-mapping>
                <servlet-mapping>
                                <servlet-name>AxisServlet</servlet-name>
                                <url-pattern>/services/*</url-pattern>
                </servlet-mapping>
                <servlet>
                                <display-name>Apache-Axis Admin Servlet Web Admin</display-name>
                                <servlet-name>AxisAdminServlet</servlet-name>
                                <servlet-class>org.apache.axis2.transport.http.AxisAdminServlet</servlet-class>
                                <load-on-startup>100</load-on-startup>
                </servlet>
                <servlet-mapping>
                                <servlet-name>AxisAdminServlet</servlet-name>
                                <url-pattern>/axis2-admin/*</url-pattern>
                </servlet-mapping>
</web-app>



Update the pom.xml with the axis2 dependencies .(Get from axis2-web)

Now Right click on pom.xml and choose Run as – Maven install to build the project. This will compile the project and execute the test cases if any and finally will produce the war as an output under target folder as well as the m2 repository in your local box. 


Console:

Build Success message  with the below info.

[INFO] Installing D:\Workspaces\Poc\SampleWebService\target\SampleWebService.war to C:\Documents and Settings\sborra\.m2\repository\com\samplews\SampleWebService\0.0.1-SNAPSHOT\SampleWebService-0.0.1-SNAPSHOT.war
[INFO] Installing D:\Workspaces\Poc\SampleWebService\pom.xml to C:\Documents and Settings\sborra\.m2\repository\com\samplews\SampleWebService\0.0.1-SNAPSHOT\SampleWebService-0.0.1-SNAPSHOT.pom


Now you are ready and deploy the war file into the server (tomcat).
Or you can deploy by copying the war file into Tomcat/Webapps 0r jboss/server/default/deploy folder and run the server. Axis2 will prepare wsdl for you and it will expose your java service as a webservice and it will give the endpoint reference to access the operations.

Now after successful deploy of the app, your webservice is ready and you can verify by using the below wsdl format.



Testing the WSDL:

We can test the available webservice through the available wsdl URL using Soap UI or direct accessing by creating a client.
Let us see test the webservice using SOAP UI.

Open the Soap UI.


Enter the Project Name: MyFirstWebService-Test

Click on OK to create.



Project will get create in SoapUI editor with the operation details as below.  Expand the list, the operation will get displayed  and expand the operation and double click on Request 1 label or right click on it and choose Show RequestEditor.


The Request editor will get open on the right side panel, with the Request Template.
Fill the request and click on Go button to hit the service endpoint, fetch the response and display in the right side to the request panel.

You can see the request and response in the below.  Now the Webservice is ready and tested. 



You can monitor the deployed web services with the following URL.


It will ask for the user name and password.
Username: admin
Password: axis2

After successful login, Home page will be displayed as below.
 Click on Available Services link to see the currently Deployed services.Here you can see the deployed and active services.
Here is the sampleCode: SampleWebService