Groovy - Funny name, serious power

Paul Zolnierczyk (@paulish29)
may 9th, 2014

In a previous post I demonstrated how to consume a stock web service using WSDL2JAVA. Although WSDL2JAVA is a great tool, it can generate some long and difficult-to-read code. In this post, I’m going to demonstrate an easier, more concise way of calling the same web service using Groovy.

Quick intro to Groovy

Groovy is a scripting language for the Java Platform. It is dynamically compiled by the JVM (Java Virtual Machine). Initially the JVM was designed with Java in mind but recently we’ve seen other languages that target the JVM hit the scene, including Scala, Clojure, and of course, Groovy. The best summary I have seen on Groovy is taken from a recent book I read:

“Groovy is an optionally typed, dynamic language for the Java platform with many features that are inspired by languages like Python, Ruby, and Smalltalk, making them available to Java developers using a Java-like syntax. Unlike other alternative languages, it’s designed as a companion, not a replacement for Java. Being Java friendly means two things: seamless integration with the Java Runtime Environment and having a syntax that’s aligned with Java.” -Dierk Koenig with Andrew Glover, Paul King, Guillaume Laforge and Jon Skeet. Groovy in Action

What resonates with me in this statement is the passage about “a companion, not a replacement”. Many times in my career I’ve come across projects that would have been better written in a different JVM language, in a different framework, or even in a different stack such as C#/.NET. However, project requirements would mandate the use of Java. With Groovy, you can still write most of your code in Java and when necessary you can include some Groovy which ultimately compiles down to JVM bytecode.

Show me the code!

Let’s get down to business. In my previous post I demonstrated using the WSDL2JAVA tool to auto-generate code to call a web service. Typically when I see “auto-generated” code it means “something convoluted I’ll never be able to understand when looking at the code”. If you had a chance to look at the code generated by WSDL2JAVA you’ll know what I mean — but the bottom line is that the code works. Again, inspired by an example in Groovy in Action, below is all the code you need to get the same exact result as the auto-generated WSDL2JAVA code.

import wslite.soap.*

def url = ''
def client = new SOAPClient(url)
def response = client.send {
version SOAPVersion.V1_2
body {
GetQuickQuote(xmlns: '') {
assert response.httpResponse.statusCode == 200
println response.GetQuickQuoteResponse.GetQuickQuoteResult

Executing this code gives a result of 507.10 (09 May 2014)

Isn’t this code a lot easier to read and maintain? If the GetQuickQuote service ever changed and required an additional String parameter you would have to run WSDL2JAVA to auto-generate the code all over again. In the above example all you would have to do is include:

NewParamater('some string')


Groovy helps make Java programming more enjoyable with almost zero learning curve (for an experienced Java developer, anyway). It makes working with SOAP web services easier and will hopefully help you save some time and headaches down the road. I suggest taking a look at the Groovy website, where you can become more familiar with Groovy and see how it may help you with your projects. Be sure and tweet me @paulish29 if Groovy has helped you in any way!

Tags: technology java web-services