Web Services: An Introduction and Creating a Java Client.

At PDF help.org we get alot of questions about Java development.

One of the most common questions raised in the last few months is about web services and how to access and implement web services using Java.

So in the first of a series of articles on web services, I will be introducing you to the concept of web services and show you how to access a simple web service within Java and PHP.

What is a Web Service?

Firstly what is a web service? A web service is a method of accessing an application via the web or the cloud using HTTP. A major advantage of this is that it allows for interoperability between different languages. For example, you can write your application in Java, then access it via a web service using PHP. This is achieved by the way data is transferred between client and service provider, SOAP, which will be further down.

What is WSDL?

Web Services Description Language, or WSDL, is an XML-based language which provides a description of the web service, similar to that of a method signature. It exposes the methods that are called, their parameters, the data structures it will return, any special datatypes, as well as information regarding accessing the services.

What is SOAP?

There are two main ways of accessing a web service; Simple object access protocol (SOAP) and representational state transfer (REST). While both carry their advantages and disadvantages, this series will be mainly covering web services using SOAP.

SOAP is a protocol for sending to data to and from the web service. It’s versatility comes from the fact that it is an XML-based protocol, which is widely supported by modern languages. The structure of the message is rather simple, it uses the namespace soap, with the root element being the Envelope. The only child element it requires is Body, which is where the data being transferred is placed, however there are other available elements for more advanced messaging.

For example, the SOAP envelope for accessing the web service http://www.webservicex.net/CurrencyConvertor.asmx?WSDL (The website is currently down unfortunately), which gets the conversion rate between two currencies looks like this:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:web="http://www.webserviceX.NET/">

The FromCurrency and ToCurrency are the inputs being sent to the web service. In this case, I’m trying to find the conversion rate from GBP to USD. Which gives this response:

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
      <ConversionRateResponse xmlns="http://www.webserviceX.NET/">

The result is given inside of the ConversionRateResult element.

A useful tool for looking at the WSDL/SOAP is SoapUI, I’m using the free version.

Writing a Basic Client

Now that we are familiar with the concept of a web service and SOAP, we can start making an application which utilises a web service. We’ll be using http://www.webservicex.net/CurrencyConvertor.asmx?WSDL again for the currency conversion. For this example I’ll be using java in NetBeans. Set up may vary depending on your IDE.

  1. Create a new project and call it CurrencyConverter
  2. Create a Java class called Converter with a main method
  3. Right click your project folder go to new and select “Web Service Client”
  4. Select WSDL URL and enter http://www.webservicex.net/CurrencyConvertor.asmx?WSDL. From there, select the package you want it to go into and press finish. It may take a few minutes to generate all of the resources of the service.
  5. Now we need to import three things from our generated web services:
    // Retrieves the SOAP interface
    import currencyconverter.webservice.CurrencyConvertor; 
    // For communicating with the server
    import currencyconverter.webservice.CurrencyConvertorSoap; 
    // Enum for currency types
    import currencyconverter.webservice.Currency;
  6. Finally, add this code to the main method:
    CurrencyConvertor cc = new CurrencyConvertor();
    // Gets an implementation of the interface we can use
    // to contact the web service.
    CurrencyConvertorSoap ccs = cc.getCurrencyConvertorSoap();
    // Send the SOAP request to the server and get the result from the web service
    double conversionRate = ccs.conversionRate(Currency.GBP, Currency.USD);
    System.out.println("£1 is worth $" + conversionRate );

    Which when executed, should give the following:

    £1 is worth $1.5518

And that’s how you make a client which contacts a web service. To demonstrate further the interoperability of webservices, here’s the same example written in PHP:

    $client = new SoapClient("http://www.webservicex.net/CurrencyConvertor.asmx?WSDL");
    // Paramters in PHP are passed via associative arrays
    $params = array("FromCurrency"=>"GBP", "ToCurrency"=>"USD");
    $result = $client->ConversionRate($params);
    echo "£1 is worth $" . $result->ConversionRateResult;

For a more advanced example of using web services, check out our cloud converter pages, which lets you see how file IO is handled with web services.

Check out the next post in this series: Web Services: Creating and Deploying a Java Web Service.

What would you like to see in future tutorials? Let us know.

If you’re a first-time reader, or simply want to be notified when we post new articles and updates, you can keep up to date by social media (TwitterFacebook and Google+) or the  Blog RSS.

The following two tabs change content below.
Simon Lissack is a developer at IDR Solutions, working on JavaFX, Android and the Cloud Conversion service.

Related Posts:


About Simon Lissack

Simon Lissack is a developer at IDR Solutions, working on JavaFX, Android and the Cloud Conversion service.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>