This usage is straightforward. Using grails let's create a simple XFire web service doing currency conversions. Let's assume, we can handle 3 currencies: AUD, USD and BGP. In order to keep things simple, we do assume a static exchange rate. Such a service would look like this:
| Code Block |
|---|
|
class TestService {
boolean transactional = false
static expose=['xfire']
static conversions = [
'AUD': [ 'USD': 100.00D, 'GBP': 44.44D ],
'USD': [ 'AUD': 1.00D, 'GBP': 88.88D ],
'GBP': [ 'AUD': 22.22D, 'USD': 33.33D ]
]
Double convert(String from, String to, Double amount) {
conversions[from][to] * amount
}
}
|
The grails project creation is quite simple:
| Code Block |
|---|
grails create-app xfire
cd xfire
grails install-plugin xfire
grails create-service test
vi grails-app/services/TestService.groovy (and paste the above code)
grails run-app
|
The wsdl is available on
| Code Block |
|---|
http://localhost:8080/xfire/services/test?wsdl |
The following groovy script consumes our brand new web service
| Code Block |
|---|
|
import groovyx.net.ws.WSClient
@Grab(group='org.codehaus.groovy.modules', module='groovyws', version='0.5.0-SNAPSHOT')
def getProxy(wsdl, classLoader) {
new WSClient(wsdl, classLoader)
}
proxy = getProxy("http://localhost:8080/xfire/services/test?wsdl", this.class.classLoader)
proxy.initialize()
result = proxy.convert("AUD", "USD", 10.0)
println "10 AUD are worth ${result} USD"
|