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:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:web="http://www.webserviceX.NET/"> <soapenv:Header/> <soapenv:Body> <web:ConversionRate> <web:FromCurrency>GBP</web:FromCurrency> <web:ToCurrency>USD</web:ToCurrency> </web:ConversionRate> </soapenv:Body> </soapenv:Envelope>
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"> <soap:Body> <ConversionRateResponse xmlns="http://www.webserviceX.NET/"> <ConversionRateResult>1.5535</ConversionRateResult> </ConversionRateResponse> </soap:Body> </soap:Envelope>
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.
- Create a new project and call it CurrencyConverter
- Create a Java class called Converter with a main method
- Right click your project folder go to new and select “Web Service Client”
- 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.
- 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;
- 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 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.
IDRsolutions develop a Java PDF library, a PDF forms to HTML5 converter, a PDF to HTML5 or SVG converter and a Java Image Library that doubles as an ImageIO replacement. On the blog our team post about anything interesting they learn about.