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

Web Services: An Introduction and Creating a Java Client.

2 min read

At IDR Solutions 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:

  

   
   
      
         GBP
         USD
      
   

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:

   
      
         1.5535
      
   

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”
    NewWSC
  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.
    NewWSC2
  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:

<!--?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 support website.

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.



Our software libraries allow you to

Convert PDF to HTML in Java
Convert PDF Forms to HTML5 in Java
Convert PDF Documents to an image in Java
Work with PDF Documents in Java
Read and Write AVIF, HEIC, WEBP and other image formats
Simon Lissack Simon Lissack is a developer at IDR Solutions, working on JavaFX, Android and the Cloud Conversion service.

4 Replies to “Web Services: An Introduction and Creating a Java Client.”

  1. This line:

    CurrencyConvertor cc = new CurrencyConvertor();

    Produces an error: “cannot instantiate the type CurrencyConvertor”

  2. Hi Neela
    if you are generating classes via NetBeans then you will get

    ConversionRate = class
    ConversionRateResponse = class
    Currency = enum
    CurrencyConvertor = class
    CurrencyConvertorHttpGet = interface
    CurrencyConvertorHttpPost = interface
    CurrencyConvertorSoap = interface
    ObjectFactory = class

    in above case you get CurrencyConvertor as java class

    * This class was generated by the JAX-WS RI.
    * JAX-WS RI 2.2.8
    * Generated source version: 2.2

    if your version is very older you may get related interface with different name

Comments are closed.