Tuesday, 3 May 2011

Google App Engine: SOAP Client Using JAX-WS

First step is to be familiarized with the wsimport tool. It is used to generate SOAP client from a WSDL file(alternative to it is to use eclipse WTP). To understand what all arguments this tool can take simply type "wsimport -help" in the console and hit enter. It will show you a list of valid arguments.
-d is used to specify the location for generated output files and -s is used to specify the location for generated source files. Refer screenshot below for the complete list of arguments.


Now its time to generate the SOAP client. Execute the following command along with your WSDL file as mentioned below:
wsimport -d "C:\gae\soap_client\bin" -s "C:\gae\soap_client\src" "http://www.webservicex.net/CurrencyConvertor.asmx?WSDL"

Once the client is generated it will look something like this:


Now here is the code for a sample servlet which calls the web service API:
package jbasics;

import java.io.IOException;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.webservicex.Currency;
import net.webservicex.CurrencyConvertor;
import net.webservicex.CurrencyConvertorSoap;

public class ServiceCaller extends HttpServlet {
    private static final long serialVersionUID = 8882886813585460639L;

    public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
 String strFromCurrency = req.getParameter("fromCurrency");
 String strToCurrency = req.getParameter("toCurrency");

 Currency fromCurrency = Currency.valueOf(strFromCurrency);
 Currency toCurrency = Currency.valueOf(strToCurrency);

 CurrencyConvertorSoap convertorSOAP = new CurrencyConvertor().getCurrencyConvertorSoap();
 double conversionRate = convertorSOAP.conversionRate(fromCurrency, toCurrency);
 System.out.println(conversionRate);

 resp.setContentType("text/plain");
 resp.getWriter().println(conversionRate);
    }
}

Add mapping for this servlet in web.xml:
<servlet>
 <servlet-name>conversionRate</servlet-name>
 <servlet-class>jbasics.ServiceCaller</servlet-class>
</servlet>
<servlet-mapping>
 <servlet-name>conversionRate</servlet-name>
 <url-pattern>/conversionRate</url-pattern>
</servlet-mapping>

Once you are done with the changes its time to deploy it to Google App Engine. After it is deployed successfully, test it with the following URL (my app url is ext-app.....you should use yours :)):
http://ext-app.appspot.com/conversionRate?fromCurrency=EUR&toCurrency=USD

Google App Engine fully supports JAX-WS for SOAP clients. So it is same as creating client at any other place. But for creating SOAP server for GAE we need to use certain tricks...will cover in upcoming posts. Stay tuned!

3 comments:

  1. I am getting following exception when I followed the steps from ur post.

    java.lang.ExceptionInInitializerError
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)

    ReplyDelete
  2. Are you using the latest version of appengine sdk (appengine-java-sdk-1.6.0.zip)?

    If not you can download from the following loation:

    http://code.google.com/appengine/downloads.html

    Also did you try running the application from your local dev environment:

    http://localhost:8888//conversionRate?fromCurrency=EUR&toCurrency=USD

    Let me know if you are still not able to run it. I will share the working project then.

    ReplyDelete
  3. I have published the source code at the following location:

    https://github.com/ranjan-rk/gae-soap-client

    ReplyDelete