Java Reference
In-Depth Information
codes under the hood, so what happens is that Groovy scripts eventually become the body
of the main method in a class that extends groovy.lang.Script .
Note two additional differences in syntax between Groovy and Java:
Semicolons are optional. Youcanaddthem, andit'sappropriate tousethem ifyou
have more than one statement on a line, but they're not normally necessary.
Parentheses are often optional. The println command is actually a method
call,andthe String istheargumenttothemethod.Because there'snoambiguity,
you can leave out the parentheses. It's not wrong to include them, though, if you
want.
Optional Parentheses
Parentheses are optional until they aren't. Simple method calls normally omit them, but if
there's any uncertainty, add them. Groovy is all about simplicity and understandability.
Now that the “Hello, World!” example is out of the way, I can move on to something a bit
more interesting. One helpful use case for Groovy is that it makes a nice client for RESTful
web services like Google Chart.
2.2. Accessing Google Chart Tools
One of the APIs that Google makes available is a RESTful web service known as the
Chart API, or, more formally, Google Chart Tools Image API. [ 1 ] The documentation is loc-
ated at https://developers.google.com/chart/image/ . The chart tools provide a rich API for
JavaScript users, but the inputs are ultimately URLs with query parameters.
1 Google officially deprecated the image charts portion of Google Chart Tools on April 20, 2012. As of summer,
2013, the API still works. It is used here both as a nice, self-contained example and as a simple application that
illustrates many Groovy features. Other examples of accessing publicly available services are given throughout the
book.
A developer sends a request to the base URL https://chart.apis.google.com/chart and ap-
pends query parameters to specify the type of chart, its size, the data, and any labels. Be-
 
Search WWH ::




Custom Search