IntroductionThis is to give a quick note about calling an External WebService from Grails. The example is turning the GroovyWS Currency Conversion example into a usable external service for your Grails Application.I wanted to be able to use the currency conversion example on GroovyWSAfter some Nabble searching and pulling a few hairs out, I found some answers (+) that I needed.I started by removing the Swing code since this will be used inside my Grails webapp and not as an application, leaving me with the following:
Not a lot there, but enough to start with.I then went to http://www.webservicex.net/CurrencyConvertor.asmx?WSDL and downloaded the wsdl so I could have a local copy, just so everything was self contained.I placed that wsdl under the WEB-INF directory and called it currencyConverter - despite the name (different, albeit correct spelling) of the asmx.Add the GroovyWS jar from the GroovyWS page to your lib directory.The wsdl has one portion that I really want and that's the ConversionRate where you can get the ConversionRate of any currency to any currency so long as they're not the same USD to CAD if you want, but not USD to USD (it's always going to be a 1:1 ratio)So in my new Groovy Class I've added Closures to do exactly what I want, below is the code that has been refined:
I also had one more library that I had to add to satisfy my application throwing a fit… apparently it wants javax.xml.soap.SOAPException and that comes in the javaee.jar which can be downloaded via a google search or it comes with IntelliJ.How you implement an external service is up to you, but this should get you started.I have inserted a lot more error checking and added the conversion utility as a Service for my Grails app as well as the ability to pass in Currency Codes for rate1 and rate2 to retrieve the current conversion rate.
import groovyx.net.ws.WSClient proxy = new WSClient("http://www.webservicex.net/CurrencyConvertor.asmx?WSDL", this.class.classLoader) def rate = proxy.ConversionRate(rate1, rate2)
import groovyx.net.ws.WSClient import org.codehaus.groovy.grails.commons.ApplicationHolderdef wsdl = ApplicationHolder.application.parentContext.getResource('WEB-INF/currencyConverter.wsdl')def proxy = new WSClient(wsdl.getURL().toString(), this.class.classLoader)def rate1 = "USD" def rate2 = "CAD"def rate = proxy.ConversionRate(rate1, rate2)



