0

Web Service Using Apache XML-RPC

Background

This articles demonstrates how to use Apache XML-RPC in Java command-line applications. We will create programs both for XML-RPC server and client.

Software Environment

  • Windows 7 Professional SP1
  • Eclipse – Kepler Release
  • Java 1.7 (1.7.0_67 – Windows x86)
  • Apache Commons Codec 1.7
    • Download from http://archive.apache.org/dist/commons/codec/. Choose file name with *current* substring.
    • Used by the XML-RPC client
  • Apache Commons HttpClient 3.0
    • Download from http://archive.apache.org/dist/httpcomponents/commons-httpclient/3.0/binary/
    • Used by the XML-RPC client
  • Apache XML-RPC 3.1.3
    • Download from https://archive.apache.org/dist/ws/xmlrpc/binaries/
  • Apache Tomcat 7.0.56
    • We’ll reference and use only the servlet-api.jar file from Tomcat.
    • Used by the XML-RPC server only

The Eclipse Project

XML-RPC is the precursor of SOAP. Before writing the codes, you need to create a Java project in Eclipse. Then, download the files and manually put them in a folder (in this case, the “lib” folder) within the project. Lastly, reference all these jar files.

xml-rpc-001

The XML-RPC Server

When creating a server, you need three (3) things:

  1. Your service class.
    It is a POJO whose methods can be invoked by remote clients, of course, via XML-RPC and through the XML-RPC server.
  2. XmlRpcServlet.properties
    This is where you register your service class so that the XML-RPC server can route RPC calls to appropriate object and method. If you have worked on Servlets, the file is like your web.xml for this command line application.IMPORTANT: This file needs to be in org.apache.xmlrpc.webserver package. Simply, create this package in your project and move or save the .properties file there.
  3. A reference to servlet-api.jar
    This is used by the ServletWebServer class

Your XML-RPC server is written something like this:

XmlRpcServlet.properties

Register your service class on this file. Note that you need a fully-qualifed name of the class. On this article, my CalculartorWebService class resides in the default package.

The XML-RPC Client

Both the server and client applications can be ran in Eclipse.

Sample Output

To test both server and client in Eclipse, open XmlRpcServer.java and run it first.

xml-rpc-001 xml-rpc-002

Once the server is up and running, go the the MyXmlRpcClient.java file and run it.

xml-rpc-003

Download the Project

https://www.dropbox.com/s/4l5bkzdnoi4ixuc/turreta%20-%20xmlrpc.zip?dl=0

 

Karl San Gabriel

Karl San Gabriel

Java and Enterprise Technologies Expert