Groovy Web Service Example

Here is a code sample that you can save to a text file and name it something like web.groovy. Copy in this code fragment :

@Grab(group='org.codehaus.groovy.modules', module='groovyws', version='0.5.2')
import groovyx.net.ws.WSClient
def ws = new groovyx.net.ws.WSClient("http://www.webservicex.net/globalweather.asmx?wsdl",this.class.classLoader)
ws.initialize()
result = ws.GetWeather('Richmond','United States')

After you run this code you should see output like :

/*
17-Jul-2011 00:15:52 org.apache.cxf.jaxb.JAXBUtils logGeneratedClassNames
INFO: Created classes: net.webservicex.GetCitiesByCountry, net.webservicex.GetCitiesByCountryResponse, net.webservicex.GetWeather, net.webservicex.GetWeatherResponse, net.webservicex.ObjectFactory
Result: <?xml version="1.0" encoding="utf-16"?>
<CurrentWeather>
  <Location>RICHMOND INTERNATIONAL  AIRPORT, VA, United States (KRIC) 37-31N 077-19W 50M</Location>
  <Time>Jul 16, 2011 - 05:54 PM EDT / 2011.07.16 2154 UTC</Time>
  <Wind> from the S (180 degrees) at 8 MPH (7 KT):0</Wind>
  <Visibility> 10 mile(s):0</Visibility>
  <SkyConditions> partly cloudy</SkyConditions>
  <Temperature> 84.9 F (29.4 C)</Temperature>
  <DewPoint> 61.0 F (16.1 C)</DewPoint>
  <RelativeHumidity> 44%</RelativeHumidity>
  <Pressure> 30.15 in. Hg (1020 hPa)</Pressure>
  <Status>Success</Status>
</CurrentWeather>
*/

References: http://docs.codehaus.org/display/GROOVY/GroovyWS

and http://groovy.codehaus.org/GroovyWS

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s