WSO2Con 2013 CFP Banner

How to Embed an Axis2 based Web Service in your Webapp?

By Davanum Srinivas
Date: Wed, 17th May, 2006
Level:
Reads: 126368 Discuss this article on Stack Overflow

Q: How to Embed an Axis2 based Web Service in your Webapp?

A: Let's try and deploy an Axis2 based simple web service in our own custom webapp. The first thing we need is an entry for the Axis2 Servlet in our web.xml. See snippet below for such an entry and a mapping for the url as well.

Davanum Srinivas

WSO2 Inc.

<web-app>
<servlet>
<servlet-name>AxisServlet</servlet-name>
<display-name>Apache-Axis Servlet</display-name>
<servlet-class>
org.apache.axis2.transport.http.AxisServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>AxisServlet</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>
</web-app>
Next, We need a simple web service. Let's pick a Book service with 2 operations findBook and getBooks Snippet from BookService.java:
    public Book[] getBooks() {
return new Book[]{onlyBook};
}

public Book findBook(String isbn) {
if (isbn.equals(onlyBook.getIsbn()))
return onlyBook;

return null;
}
Next we need a deployment descriptor for the BookService. As you can see, you can deploy a POJO using RPCMessageReceiver and you can specify a namespace for the schema as well as for the web service itself.
<serviceGroup>
<service name="BookService"
targetNamespace="http://ws.apache.org/axis2/samples/book/">
<description>Book sample service</description>
<schema schemaNamespace="http://ws.apache.org/axis2/samples/book/xsd/"/>
<parameter name="ServiceClass" locked="false">samples.demo.BookService</parameter>
<operation name="getBooks">
<messageReceiver class="org.apache.axis2.rpc.receivers.RPCMessageReceiver"/>
</operation>
<operation name="findBook">
<messageReceiver class="org.apache.axis2.rpc.receivers.RPCMessageReceiver"/>
</operation>
</service>
</serviceGroup>
Next, do we really need to create an aar? An aar is useful for hot deployment and service isolation, but this is an embedded scenario, so we go with the "Exploded deployment option" which means that we create a directory structure inside our WEB-INF as follows:
\---WEB-INF
| web.xml
|
\---services
\---BookService
\---META-INF
services.xml
Usually we would have dropped a BookService.aar in the services directory, here we create a directory named BookService and drop the deployment descriptor (services.xml) inside the META-INF directory under it. Hmm, did we forget something? Yes, how about an easy build environment using maven2? Here's the layout of our files for maven2.
\---book
| pom.xml
| README
|
\---src
+---main
| | log4j.properties
| |
| \---samples
| \---demo
| Book.java
| BookService.java
|
+---test
\---webapp
\---WEB-INF
| web.xml
|
\---services
\---BookService
\---META-INF
services.xml
All the jars needed as dependencies are automatically downloaded by maven2 from the Apache and Ibiblio respositories. So you don't even have to download Axis2 dist to build and deploy this sample. All you need to do is unzip the zip below and run
$ mvn install war:war
then rename the resultant war as axis2.war and drop it into any servlet engine. Once your servlet engine starts, you can view the wsdl for your service at:
http://yourhost:port/axis2/services/BookService?wsdl

Enjoy! Please drop an email to axis-dev@ws.apache.org if you run into problems or have comments.

Please download the entire zip here: book.zip

WSO2Con 2014