Showing posts with label Apache Axis2. Show all posts
Showing posts with label Apache Axis2. Show all posts

January 15, 2012

Deploying an Apache Axis2 Web Service - Hello World Example

1) Setup Apache Axis2 in the web application - follow the steps here

2) Create a POJO - HelloWorldService.java - in the src folder of the web application

package info.icontraining.ws.axis2;

public class HelloWorld {
   public String sayHello(String name) {
      return "Hello " + name;
   }
}

3) In the WebContent/WEB-INF/services folder, create a new folder and name it 'HelloWorld'.
In this folder create a sub-folder and name it 'META-INF'.
In the META-INF folder create a new XML file - name it 'services.xml'.

4) Add the following configuration to the services.xml file created in the step above,

<service>
   <parameter name="ServiceClass" locked="false">info.icontraining.ws.axis2.HelloWorld</parameter>
   <operation name="sayHello">
      <messageReceiver class="org.apache.axis2.rpc.receivers.RPCMessageReceiver"/>
   </operation> 
</service>

5) Now go to the build/classes folder of the web application. Copy the folder named 'info' and paste it into the services/HelloWorld folder created in step 3.

So, now in the services/HelloWorld folder, there are 2 folders - META-INF and info
The META-INF folder contains the services.xml file.
The info folder contains the following hierarchy of sub-folders - icontraining/ws/axis2/HelloWorldService.class

6) Deploy the Web Application to the server, and access the following URL in the browser,

http://localhost:8080/WebAppName/services/listServices

The HelloWorld service will be present in the list of services.

7) Invoke the Web Service with the following URL in the browser,

http://localhost:8080/WebAppName/services/HelloWorld/sayHello?name=dinesh

Setting up Apache Axis2 in a Web Application

1) Download the required resources for the Apache Axis2 installation from the link here. Unzip the contents of the downloaded zip file.

2) Copy all the jars in the lib folder in the zip file into the WebContent/WEB-INF/lib folder of the web application.

3) Copy the folder axis2-web (and its contents) into the WebContent folder of the web application

4) Copy the conf, modules and services folders into the WebContent/WEB-INF folder of the web application

5) The structure of the Web Application project after the above steps will look like this,

WebAppName
   |
   |--- WebContent
            |
            |--- axis2-web (and all contents of this folder)
            |
            |---WEB-INF
                   |
                   |--- conf (axis2.xml file in this folder)
                   |
                   |--- lib (all jars files in this folder)
                   |
                   |--- modules (*.mar files in this folder)
                   |
                   |--- services (and its contents in this folder)

6) Add the following configuration to the WebContent/WEB-INF/web.xml file of the web application and within its root <web-app> element

<servlet>
   <servlet-name>AxisServlet</servlet-name>
   <servlet-class>org.apache.axis2.transport.http.AxisServlet</servlet-class>
   <load-on-startup>1</load-on-startup>
</servlet>
<servlet>
   <servlet-name>AxisAdminServlet</servlet-name>
   <servlet-class>org.apache.axis2.webapp.AxisAdminServlet</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-mapping>
   <servlet-name>AxisAdminServlet</servlet-name>
   <url-pattern>/axis2-admin/*</url-pattern>
</servlet-mapping>

<mime-mapping>
   <extension>inc</extension>
   <mime-type>text/plain</mime-type>
</mime-mapping>

<welcome-file-list>
   <welcome-file>index.jsp</welcome-file>
   <welcome-file>index.html</welcome-file>
   <welcome-file>/axis2-web/index.jsp</welcome-file>
</welcome-file-list>

<error-page>
   <error-code>404</error-code>
   <location>/axis2-web/Error/error404.jsp</location>
</error-page>

<error-page>
   <error-code>500</error-code>
   <location>/axis2-web/Error/error500.jsp</location>
</error-page>


7) Deploy the Web Application to the server. Test the installation was successful by accessing the following URL through the browser,

http://localhost:8080/WebAppName/axis2-web/index.jsp

8) Click on the links - Services, Validate and Administration - and check each of the links work.

Note: Use admin/axis2 as username/password to log in as Administrator

Note: If Validate link displays an error message for the Version service, resolve the error from this post here

9) Invoke the Version Web Service with this URL in the browser:

http://localhost:8080/WebAppName/services/Version/getVersionRequest

getVersion Service method error in Apache Axis2 - There was a problem in Axis2 version service , may be the service not available or some thing has gone wrong.

Topic: Apache Axis2

Application Server: JBoss 4.2.2GA

Exception:

Clicking on the Validate link during Apache Axis2 installation shows the following error message:

There was a problem in Axis2 version service , may be the service not available or some thing has gone wrong. But this does not mean system is not working ! Try to upload some other service and check to see whether it is working.

and shows the following stack trace on the server console log:

14:20:11,065 INFO  [STDOUT] 14:20:11,065 ERROR [RPCMessageReceiver] Exception occurred while trying to invoke service method getVersion
org.apache.axis2.AxisFault: namespace mismatch require http://axisversion.sample found http://axisversion.sample/xsd
 at org.apache.axis2.rpc.receivers.RPCUtil.invokeServiceClass(RPCUtil.java:190)
 at org.apache.axis2.rpc.receivers.RPCMessageReceiver.invokeBusinessLogic(RPCMessageReceiver.java:117)
 at org.apache.axis2.receivers.AbstractInOutMessageReceiver.invokeBusinessLogic(AbstractInOutMessageReceiver.java:40)
 at org.apache.axis2.receivers.AbstractMessageReceiver.receive(AbstractMessageReceiver.java:110)
 at org.apache.axis2.engine.AxisEngine.receive(AxisEngine.java:181)
 at org.apache.axis2.transport.http.HTTPTransportUtils.processHTTPPostRequest(HTTPTransportUtils.java:172)
 at org.apache.axis2.transport.http.AxisServlet.doPost(AxisServlet.java:146)
 at javax.servlet.http.HttpServlet.service(HttpServlet.java:710)
 at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
 at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
 at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
 at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)
 at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
 at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
 at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:230)
 at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
 at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:179)
 at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:84)
 at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
 at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
 at org.jboss.web.tomcat.service.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:157)
 at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
 at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:262)
 at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
 at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
 at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:446)
 at java.lang.Thread.run(Unknown Source)

Resolution:

Open the axis2-web/HappyAxis.jsp and find the following line of code:

OMNamespace omNs = fac.createOMNamespace("http://axisversion.sample/xsd", "ns1");

Modify the above line to the following:

OMNamespace omNs = fac.createOMNamespace("http://axisversion.sample", "ns1");