Srikanth Technologies

Java Client for WebServiceX.Net Web Service

Here is a simple client in Java to access currency conversion web service provided by webservicex.net.

This example shows how web services provide interoperability as web service provided by webservicex.net is written in .NET technology, but we are using Java client to access it. In fact, a web service created with one technology can be accessed by clients in any other technology.

Follow the steps given below to access currency conversion web service:
  1. Go to webservicex.net and select Currency Conversion web service. You get a page with the details of the web service.
  2. Find out WSDL Schema location, which is a URL, for the web service in that page. For currency conversion web service, schema location is : http://www.webservicex.net/CurrencyConvertor.asmx?WSDL
  3. Create a new project in NetBeans IDE. I created a simple Java Application with the name webservicexclient.
  4. Create a proxy for web service by adding Web Service Client to your project by selecting File -> New File -> Web Services (Category) -> Web Service Client.
  5. It displays New Web Service Client window. Select WSDL URL radio button and enter the URL provided by web page in webservicex.net.
  6. Enter package name as webservicexnet.
  7. Client style is already selected as JAX-WS Style, so leave it as it is.
  8. Click on Finish button and it takes a few seconds to minutes to generate proxy.
  9. Create a Java Class with main function. Use Insert Code option from context menu and select Call Web Service Operation option. Expand the nodes and select ConversionRate option from CurrencyConvertorSoap12 node, so that code to call ConversionRate method in web service using SOAP 1.2 is generated. Then call the function generated by NetBeans IDE.
  10. The code is shown below:
    import webservicexnet.Currency;
    
    public class CurrencyConversionClient {
    
        public static void main(String[] args) {
       
                double inr  = 10000;
                double rate = conversionRate( Currency.INR,Currency.USD);
                System.out.printf( "USD for %f INR = %f\n", inr, inr * rate);
                
        }
    
        private static double conversionRate(webservicexnet.Currency fromCurrency, webservicexnet.Currency toCurrency) {
            webservicexnet.CurrencyConvertor service = new webservicexnet.CurrencyConvertor();
            webservicexnet.CurrencyConvertorSoap port = service.getCurrencyConvertorSoap12();
            return port.conversionRate(fromCurrency, toCurrency);
        }
    }
    
    Method conversionRate() takes enumeration for from currency and to currency. We pass INR for fromcurrency and USD for tocurrency. Once we obtain conversion rate then we multiply the value in INR with conversion rate to get equivalent USD value.
  11. Clean and Build the project to ensure all classes generated by NetBeans IDE for proxy are compiled successfully.
  12. Run the program using Context Menu -> Run File. I have hard-coded INR value for simplicity; however you can take the value from user. It is also possible to call web service operation from a web application, if you so desire.
The aim of this blog is to show how you can call a live web service using simple Java code. I recommend you try accessing some other web services provided by webservicex.net using the process given in this blog.