September 21, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Exposing a Database as a Web Service

  • March 21, 2008
  • By Deepal Jayasinghe
  • Send Email »
  • More Articles »

The service description file, or the services.xml for your service, will be a very simple one. You can download that from the Download section. The services.xml file will look like the following:

<service name="DBSampleService"
         class="dbsample.DBSampleServiceLifeCycle">
   <description>Exposing a DB as a Web Service</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">
      dbsample.PersonDBService
   </parameter>
</service>

Deploying the Service

Before you deploy the service, you need to create a service archive file using your compiled classes and services.xml. You can use any available tools, or you can just create a zip file from the compiled code and services.xml and rename that as dbsample.aar. I have created a service archive file from the compiled code so that you can download that and just try it out.

Because you need to have the DB connection jar, first you have to copy the mysql-connector jar file in the class path or to <TOMCATHOME>/webappes/axis2/WEB-INF/lib. Next, you can copy your service archive file into <TOMCATHOME>/webappes/axis2/WEB-INF/services directory. Then start Tomcat (or your application server).

Now, type http://localhost:8080/axis2/services/DBSampleService?wsdl in your browser (the port may vary depending on the application server configurations); then, you will be able to see the WSDL file for your sample DB service. This is simply an indicator that your service is up and running. If you do not get the WSDL file, something has gone wrong with your database or database driver.

Invoking the Service

In this sample, you are not going to write a Java client to invoke the service; if you want, you can try that out. However, you will focus more on invoking the service just using the browser or REST manner.

List all the People in the DB

To see all the people in the database, you can invoke the "listAllPeople" method in your service. Just type the following in the browser and see what you are getting. (This is how you invoke the listAllPeople method in the REST manner.)

http://localhost:8080/axis2/services/DBSampleService/listAllPeople

You will get something like the following, which is simply all the people in the DB.

<ns:listAllPeopleResponse>
   <ns:return type="dbsample.Person">
      <ax21:address>
         No 59, Flower Road, Colombo, Sir Lanka
      </ax21:address>
      <ax21:age>29</ax21:age>
      <ax21:id>100</ax21:id>
      <ax21:name>Deepal</ax21:name>
   </ns:return>
   <ns:return type="dbsample.Person">
      <ax21:address>San Jose, CA</ax21:address>
      <ax21:age>30</ax21:age>
      <ax21:id>101</ax21:id>
      <ax21:name>Franck</ax21:name>
   </ns:return>
   <ns:return type="dbsample.Person">
      <ax21:address> Colombo, Sri Lanka</ax21:address>
      <ax21:age>34</ax21:age>
      <ax21:id>102</ax21:id>
      <ax21:name>Samisa Abeysinghe</ax21:name>
   </ns:return>
</ns:listAllPeopleResponse>

List the Names of All the People in the DB

Getting a list of names of the people is almost equal to the method invocation above. To get all the names, just type the following in the browser and see what you get:

http://localhost:8080/axis2/services/DBSampleService/listPeopleNames

Then, you will see something like the following, which is exactly all the names of the people in the DB.

<ns:listPeopleNamesResponse>
   <ns:return>Deepal</ns:return>
   <ns:return>Franck</ns:return>
   <ns:return>Samisa Abeysinghe</ns:return>
</ns:listPeopleNamesResponse>

Getting the Name and Age for a Given Person

Now, you will the name and age of a given person, so will be a matter of giving the person ID and the Web Service gives you the name and age of the person represented by the id. Invoking that service is just a matter or typing the following in the browser:

http://localhost:8080/axis2/services/DBSampleService/getNameAge?id=100

Then, you get the following output:

<ns:getNameAgeResponse>
   <ns:return type="dbsample.NameAge">
      <ax21:age>29</ax21:age>
      <ax21:name>Deepal</ax21:name>
   </ns:return>
</ns:getNameAgeResponse>

Tags: Web services



Page 2 of 3



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel