First steps with Groovy

Inspired by Scott Davies and his great talk during Java Developers’ Day in Krakow I decided to try out Groovy. As a first step I want to create a running application which contains both Java and Groovy classes talking to each other. I’ll also take Scott’s idea to use a Geocoding RESTful web service to show how simple dealing with XML in Groovy is. So – let’s start.

  • Step 0 – Download software

Groovy, Eclipse plug-in,

  • Step 1 – create Maven project with GMaven

mvn archetype:generate -DarchetypeGroupId=org.codehaus.groovy.maven.archetypes -DarchetypeArtifactId=gmaven-archetype-basic -DarchetypeVersion=1.0-rc-5 -DgroupId=com.krzysztofadamczyk.playground.geocoder -DartifactId=geocoder

  • Step 2 – build project & import to Eclipse

Add “java” directories to both src/main and src/test directories

mvn install

mvn -Declipse.downloadSources eclipse:eclipse

  • Step 3 – implement domain class in Java
package com.krzysztofadamczyk.playground.geocoder.domain;

public class Location {

    private final double longitude;
    private final double latitude;

    public Location(final double longitude, final double latitude) {
        this.longitude = longitude;
        this.latitude = latitude;
    }

    public double getLongitude() {
        return this.longitude;
    }

    public double getLatitude() {
        return this.latitude;
    }

    public String toString() {
        return "Location [latitude=" + this.latitude + ", longitude=" + this.longitude + "]";
    }

}
  • Step 4 – implement web service client in Groovy
package com.krzysztofadamczyk.playground.geocoder

import com.krzysztofadamczyk.playground.geocoder.domain.Location
public class GroovyGeocoder{
	private final def BASEURL = "http://worldkit.org/geocoder/rest/?city=";

	public def Location getLocation(String city, String twoLetterCountryCode) {
		def wsAddress = BASEURL + city + "," + twoLetterCountryCode
		def geocoded = wsAddress.toURL().text
		def RDF = new XmlSlurper().parseText(geocoded)

		new Location(Double.valueOf(RDF.Point.long.text() ), Double.valueOf(RDF.Point.lat.text()))
	}

}
  • Step 5 – Implement main class in Java
package com.krzysztofadamczyk.playground.geocoder.runner;

import com.krzysztofadamczyk.playground.geocoder.GroovyGeocoder;
import com.krzysztofadamczyk.playground.geocoder.domain.Location;

public class Runner {
    public static void main(final String[] args) {
        final GroovyGeocoder geocoder = new GroovyGeocoder();
        final Location location = geocoder.getLocation("Krakow", "PL");
        System.out.println(location);
    }

}

Running this simple example produces the following output:

Location [latitude=50.0833333, longitude=19.9166667]
Advertisement

One Response to First steps with Groovy

  1. Pingback: Create Groovy Designs

Leave a Reply

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

Gravatar
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 )

Connecting to %s

Follow

Get every new post delivered to your Inbox.