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

Story :

Project :

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.

<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

3
Average: 3 (1 vote)

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Very helpful, thanks

This is one of the most useful articles I've seen here, not least because of the provision of a Maven2 POM which helps get all the dependencies in place. Also, it's very concise, heavy on example code, and above all it works!

Weblogic 8.1SP4

I tried your sample and it does not work!

I tried also with axis1.1.1 and the services and modules list are empty!

The only way I succeed to have embeded services is to place the services.xml directly in the WEB-INF directory. But I Axis2 is unbale to load the services and modules respectively from the WEB-INF/services and WEB-INF/modules. I tried to explode also the aar and the mar with the same result. the other problem is that when I use the ?wsd parameter on the service, I obtain an error telling to put the wsdl in the META-INF and it is the case!

Did someone succed in deploying the axis2 engine with embeded new service from a war file (not exploded) in Weblogic 8.1?

Embedding multiple WS?

Could you please also explain the layout, especially regarding services.xml location, for a case where there's a service group containig multiple services. I have to expose several classes in an existing application as webservices; it would be most convenient to keep corresponding services separately.

 Thank you,
Alex.

How to Embed an Axis2 based

Hi Dims,

 

This is very nice and usefule article . But i am not able to download book.zip. Could you send me the book.zip? Thanks.

 

Hailong

Oops!

Looks like the url got moved. try this one for now till we fix the file location on the server-side. <br>

http://wso2.org/article-old/book.zip

Still no book.zip

Hi,

I still can't get the example.

I can download the book.zip file from the article but it only contains META-INF/Manifest file.

I tried to download the zip from that other link, but I get a 404 Not Found page. :(

How to Embed an Axis2 based

I found the next problem when I access to
http://localhost:5800/axis2/services/BookService

problem: Please enable REST support in WEB-INF/conf/axis2.xml and WEB-INF/web.xml

I don't know what it means, somebody can help me please

Sotes

Deployment Problem

Hi guys,

when I feed my tomcat (5.5.20) with the successfully build war from the book.zip I get following error:

Jul 27, 2007 1:30:29 PM org.apache.axis2.deployment.ServiceBuilder populateService
SEVERE: Error in schema generating samples.demo.BookService
java.lang.ClassNotFoundException: samples.demo.BookService
at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:242)
at org.apache.ws.java2wsdl.SchemaGenerator.(SchemaGenerator.java:70)
at org.apache.axis2.deployment.util.Utils.fillAxisService(Utils.java:209)
at org.apache.axis2.deployment.ServiceBuilder.populateService(ServiceBuilder.java:149)
at org.apache.axis2.deployment.ServiceGroupBuilder.populateServiceGroup(ServiceGroupBuilder.java:91)
at org.apache.axis2.deployment.repository.util.ArchiveReader.buildServiceGroup(ArchiveReader.java:84)
at org.apache.axis2.deployment.repository.util.ArchiveReader.processServiceGroup(ArchiveReader.java:145)
at org.apache.axis2.deployment.DeploymentEngine.doDeploy(DeploymentEngine.java:620)
at org.apache.axis2.deployment.repository.util.WSInfoList.update(WSInfoList.java:195)
at org.apache.axis2.deployment.RepositoryListener.update(RepositoryListener.java:207)
at org.apache.axis2.deployment.RepositoryListener.checkServices(RepositoryListener.java:155)
at org.apache.axis2.deployment.DeploymentEngine.loadServices(DeploymentEngine.java:91)
at org.apache.axis2.deployment.WarBasedAxisConfigurator.loadServices(WarBasedAxisConfigurator.java:142)
...

Any ideas?

Cheers,
Dirk

This worked for me too.

This worked for me too (Axis2 1.3, Tomcat 5.5.23). The jars went to webapp's lib directory. Now it's fully spring loaded. Thanks.

I tried a similar excersize with CXF and did not have much luck based on their documentation. The sad part is that there isn't as much following to that project as there is after Axis2. It makes it hard locating other working examples of the basic setup.

 

class not found error

the problem is axis can't found the service class BookService, check the ubication of the file

 

Sotes

Undeploying, hot deploying embedded Axis2 service

What's up with Axis2 classloader? When i undeploy my webapp, the jars stick around, and hence a full undeployment doesn't occur. A redeployment can't occur after that either. I end up recycling the server. Any possible programmatic work around? Am i supposed to do some classloading trick...something was mentioned on Axis2's website about that when running "inside the aar".

 

Rename axis2.war ?

Hi! Great tutorial!
I managed to create the "axis2.war" and it runs perfectly.
But now I want to rename it - say into "book.war". When I do so I can address the wsdl at "http://yourhost:port/book/services/BookService?wsdl".

So far so good- but how can I make Axis to generate in the wsdl the endpoints correctly? These unfortunately stay at:

soap:address location="http://yourhost:port/axis2/services/BookService" .

Many thanks in advance!

Best,
George

Jsp problem

I followed the instructions in this, and I get:

 

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

javax.servlet.ServletException: File "/axis2-web/listSingleService.jsp" not found
org.apache.axis2.transport.http.AxisServlet.doGet(AxisServlet.java:111)
javax.servlet.http.HttpServlet.service(HttpServlet.java:690)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)

root cause

javax.servlet.ServletException: File "/axis2-web/listSingleService.jsp" not found
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:308)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:265)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
org.apache.axis2.transport.http.AbstractAgent.renderView(AbstractAgent.java:116)
org.apache.axis2.transport.http.ListingAgent.processListService(ListingAgent.java:197)
org.apache.axis2.transport.http.ListingAgent.handle(ListingAgent.java:89)
org.apache.axis2.transport.http.AxisServlet.doGet(AxisServlet.java:109)
javax.servlet.http.HttpServlet.service(HttpServlet.java:690)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
 
 

I also get this when trying with a maven-less build. I understand that the jsps are missing, but why, shouldn't maven have magically included them?

 

Also, I tried adding them manually to my alternative, non-maven try and that did not seem to help. I saw a reference saying I should simply add the axis2-web folder under my WEB-INF.

Did you ever solved this problem?

Hello!
I just wonder if you ever solved this problem?
If you solved it how did you do it?

Best regards
Fredrik

Axis2 1.3 compatible with WebSphere 6.0.2.x?

 

 
Hello,
            Is Axis2 1.3 compatible with WebSphere 6.0.2.x?  I found an example, http://wso2.org/library/90 , which works like a champ on Tomcat 5.5 where I can deploy it and hit the wsdl.  The problem is trying to hit the wsdl with WebSphere 6.0.2.x, I just get a 404.  I have looked at the server logs and it starts fine with no errors.  The only suspicious entries in the log are
 
[3/26/08 16:42:55:934 EDT] 00000021 ApplicationMg A   WSVR0200I: Starting application: axis2_war
[3/26/08 16:42:56:293 EDT] 00000021 WebGroup      A   SRVE0169I: Loading Web Module: axis2.war.
[3/26/08 16:42:57:527 EDT] 00000021 DeploymentEng I org.apache.axis2.deployment.DeploymentEngine prepareRepository no modules directory found , new one created
[3/26/08 16:42:57:871 EDT] 00000021 SystemOut     O [JAM] Warning: You are running under a pre-1.5 JDK.  JSR175-style source annotations will not be available
[3/26/08 16:42:58:043 EDT] 00000021 ServletWrappe A   SRVE0242I: [axis2_war] [/axis2] [AxisServlet]: Initialization successful.
[3/26/08 16:42:58:059 EDT] 00000021 VirtualHost   I   SRVE0250I: Web Module <null> has been bound to default_host[*:9080,*:80,*:9443].
[3/26/08 16:42:58:074 EDT] 00000021 ApplicationMg A   WSVR0221I: Application started: axis2_war
 
Any help or information would be greatly appreciated

jsp problem

hi

i am getting same error what is solution of this error

Does not work on OC4J either

I am trying to deploy the book.zip on an OC4J (10.1.3.1) container. After building it successfully, I deploy it. The OC4J container says deployment went fine. But when I access the wsdl mentioned above I get the 404- /axis2/services/BookService not found Error. It looks like OC4j is trying to look for a listServices.jsp file and of course it is not there? Why would this happen and I how would I fix it? Any suggestions?

Can you give me some more guidance on this topic?

Hi,

Thanks for this great and useful article.

Recently I need to add a SOAP server service using Axis2 to my web application.
I think the approach mentioned in your article is what I need, but I don't understand it very well.
Would you be so kind to give me some guidance?

My questions are.

1. Are those mentioned in yoru article the only things need to be done to embed Axis2 to my web applicaiton?
2. Since I'm adding a SOAP server serivce, is there anything more I should do?
For example, where to define the SOAPAction etc.?
3. There is a command '$ mvn install war:war' to build the war file.
I'm using Windows XP, can I do the same thing?

I'm a newbie on Axis. I'd really appreciate it if you can elaborate more on this topic.

thansk in advance,

-Tian Chi

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.