Friday, October 1, 2010

Start to develop web service - part 1

The article will show you the fastest way to experience web service with AXIS2.
if you want to learn more about web service and axis2, pls refer to http://ws.apache.org/axis2/index.html
I assume your tomcat is ready.
step 1. download war distribution from here http://ws.apache.org/axis2/download/1_4_1/download.cgi#std-bin
step 2. unzip this war file under %tomcat_home%/webapps, the folder structure maybe
c:\tomcat6
       |- webapps
              |- axis2
                       |- axis2-web
                       |- META-INF
                       |- WEB-INF
step 3. verify axis2 installation is ok.  key in the URL http://localhost:8080/axis2/  in your browser.
if you see the welcome page, the installation is ok.
stetp 4. create a class as follows.
SimepleService.java
public class SimpleService
{
   public String getGreeting(String name)
   {
     return "hello " + name;
   }

   public int getPrice()
   {
      return new java.util.Random().nextInt(1000);
   }
}

Note: no package here.
step 5. build the class and copy SimpleService.class with the whole folder to
%tomcat_home%\webapps\axis2\WEB-INF\services
 step 6. verify the SimpleService.class has been deployed as web service.
      key in URL http://localhost:8080/axis2/services/listServices in your browser, you will see the service is deployed.



step 7. verify the services can work.
  Type the following URL in your browser:
 http://localhost:8080/axis2/services/SimpleService/getGreeting?name=bill
 http://localhost:8080/axis2/services/SimpleService/getPrice\\
Take Note:
<parameter name="hotdeployment">true</parameter>
ensure this setting is in your %tomcat_home%\webapps\axis2\WEB-INF\conf\axis2.xml
if you change the class, just refresh page to get the changes, no need to restart tomcat.
this setting is convenient in development stage.
===================================
you also can copy the .class to %tomcat_home%\webapps\axis2\WEB-INF\pojo  folder
 <!-POJO deployer , this will alow users to drop .class file and make that into a service->
    <deployer extension=".class" directory="pojo" class="org.apache.axis2.deployment.POJODeployer"/>
    <deployer extension=".jar" directory="servicejars" class="org.apache.axis2.jaxws.framework.JAXWSDeployer"/>
check it in axis2.xml, this is a default method.
of course, you can change directory="pojo" to another folder, let's say directory="myfolder"
then you need to copy .class file to %tomcat_home%\webapps\axis2\WEB-INF\myfolder

No comments:

Post a Comment