
- •brief contents
- •contents
- •foreword
- •preface
- •acknowledgments
- •about this book
- •Roadmap
- •Code conventions and downloads
- •Author Online
- •About the author
- •about the cover illustration
- •1 Why add Groovy to Java?
- •1.1 Issues with Java
- •1.1.1 Is static typing a bug or a feature?
- •1.1.2 Methods must be in a class, even if you don’t need or want one
- •1.1.3 Java is overly verbose
- •1.1.4 Groovy makes testing Java much easier
- •1.1.5 Groovy tools simplify your build
- •1.2 Groovy features that help Java
- •1.3 Java use cases and how Groovy helps
- •1.3.1 Spring framework support for Groovy
- •1.3.2 Simplified database access
- •1.3.3 Building and accessing web services
- •1.3.4 Web application enhancements
- •1.4 Summary
- •2 Groovy by example
- •2.1 Hello, Groovy
- •2.2 Accessing Google Chart Tools
- •2.2.1 Assembling the URL with query string
- •2.2.2 Transmitting the URL
- •2.2.3 Creating a UI with SwingBuilder
- •2.3 Groovy Baseball
- •2.3.1 Database data and Plain Old Groovy Objects
- •2.3.2 Parsing XML
- •2.3.3 HTML builders and groovlets
- •2.4 Summary
- •3 Code-level integration
- •3.1 Integrating Java with other languages
- •3.2 Executing Groovy scripts from Java
- •3.2.1 Using JSR223 scripting for the Java Platform API
- •3.2.2 Working with the Groovy Eval class
- •3.2.3 Working with the GroovyShell class
- •3.2.4 Calling Groovy from Java the easy way
- •3.2.5 Calling Java from Groovy
- •3.3 Summary
- •4 Using Groovy features in Java
- •4.1 Treating POJOs like POGOs
- •4.2 Implementing operator overloading in Java
- •4.3 Making Java library classes better: the Groovy JDK
- •4.4 Cool AST transformations
- •4.4.1 Delegating to contained objects
- •4.4.2 Creating immutable objects
- •4.4.3 Creating singletons
- •4.5 Working with XML
- •4.6 Working with JSON data
- •4.7 Summary
- •5 Build processes
- •5.1 The build challenge
- •5.2 The Java approach, part 1: Ant
- •5.3 Making Ant Groovy
- •5.3.1 The <groovy> Ant task
- •5.3.2 The <groovyc> Ant task
- •5.3.3 Writing your build in Groovy with AntBuilder
- •5.3.4 Custom build scripts with Gant
- •5.3.5 Ant summary
- •5.4 The Java approach, part 2: Maven
- •5.4.2 The GMaven project
- •5.4.3 Maven summary
- •5.5 Grapes and @Grab
- •5.6 The Gradle build system
- •5.6.1 Basic Gradle builds
- •5.6.2 Interesting configurations
- •5.7 Summary
- •6 Testing Groovy and Java projects
- •6.1 Working with JUnit
- •6.1.1 A Java test for the Groovy implementation
- •6.1.2 A Groovy test for the Java implementation
- •6.1.3 A GroovyTestCase test for a Java implementation
- •6.2 Testing scripts written in Groovy
- •6.2.1 Useful subclasses of GroovyTestCase: GroovyShellTestCase
- •6.2.2 Useful subclasses of GroovyTestCase: GroovyLogTestCase
- •6.3 Testing classes in isolation
- •6.3.1 Coerced closures
- •6.3.2 The Expando class
- •6.3.3 StubFor and MockFor
- •6.4 The future of testing: Spock
- •6.4.1 The Search for Spock
- •6.4.2 Test well, and prosper
- •6.4.4 The trouble with tribbles
- •6.4.5 Other Spock capabilities
- •6.5 Summary
- •7 The Spring framework
- •7.1 A Spring application
- •7.2 Refreshable beans
- •7.3 Spring AOP with Groovy beans
- •7.4 Inline scripted beans
- •7.5 Groovy with JavaConfig
- •7.6 Building beans with the Grails BeanBuilder
- •7.7 Summary
- •8 Database access
- •8.1 The Java approach, part 1: JDBC
- •8.2 The Groovy approach, part 1: groovy.sql.Sql
- •8.3 The Java approach, part 2: Hibernate and JPA
- •8.4 The Groovy approach, part 2: Groovy and GORM
- •8.4.1 Groovy simplifications
- •8.5 Groovy and NoSQL databases
- •8.5.1 Populating Groovy vampires
- •8.5.2 Querying and mapping MongoDB data
- •8.6 Summary
- •9 RESTful web services
- •9.1 The REST architecture
- •9.3 Implementing JAX-RS with Groovy
- •9.4 RESTful Clients
- •9.5 Hypermedia
- •9.5.1 A simple example: Rotten Tomatoes
- •9.5.2 Adding transitional links
- •9.5.3 Adding structural links
- •9.5.4 Using a JsonBuilder to control the output
- •9.6 Other Groovy approaches
- •9.6.1 Groovlets
- •9.6.2 Ratpack
- •9.6.3 Grails and REST
- •9.7 Summary
- •10 Building and testing web applications
- •10.1 Groovy servlets and ServletCategory
- •10.2 Easy server-side development with groovlets
- •10.2.1 A “Hello, World!” groovlet
- •10.2.2 Implicit variables in groovlets
- •10.3.2 Integration testing with Gradle
- •10.3.3 Automating Jetty in the Gradle build
- •10.4 Grails: the Groovy “killer app”
- •10.4.1 The quest for the holy Grails
- •10.5 Summary
- •A.1 Installing a JDK
- •A.2 Installing Groovy
- •A.3 Testing your installation
- •A.4 IDE support
- •A.5 Installing other projects in the Groovy ecosystem
- •B.1 Scripts and the traditional example
- •B.2 Variables, numbers, and strings
- •B.2.1 Numbers
- •B.2.2 Strings and Groovy strings
- •B.3 Plain Old Groovy Objects
- •B.4 Collections
- •B.4.1 Ranges
- •B.4.2 Lists
- •B.4.3 Maps
- •B.5 Closures
- •B.6 Loops and conditionals
- •B.6.1 Loops
- •B.6.2 Conditionals
- •B.6.3 Elvis
- •B.6.4 Safe de-reference
- •B.7 File I/O
- •B.8.1 Parsing and slurping XML
- •B.8.2 Generating XML
- •B.8.3 Validation
- •B.9 JSON support
- •B.9.1 Slurping JSON
- •B.9.2 Building JSON
- •index
- •Symbols
Java use cases and how Groovy helps |
15 |
that I used for strings was a simple example. You can also use Java classes with Groovy and add features like operator overloading to Java. These and related topics will be discussed in chapter 4.
2Groovy uses Java libraries.
Practically every Groovy class relies on the Java libraries, with or without Groovy additions. That means virtually every Groovy class is already an integration story, mixing Groovy and Java together. One nice use case for Groovy is to experiment with Java libraries you haven’t used before.
3Groovy makes working with XML and JSON easy.
Here’s an area where Groovy shines. Groovy includes classes called MarkupBuilder, which makes it easy to generate XML, and JsonBuilder, which produces JSON objects. It also has classes called XmlParser and XmlSlurper, which convert XML data structures into DOM structures in memory, and JsonSlurper, to parse JSON data. These will be used throughout the book, especially in chapter 9 on RESTful web services.
4Groovy includes simplified data source manipulation.
The groovy.sql.Sql class provides a very simple way to work with relational databases. I’ll talk about this in chapter 8 on databases, chapter 7 on working with the Spring framework, and chapter 9 on RESTful web services.
5Groovy’s metaprogramming streamlines development.
The builder classes are an example of Groovy metaprogramming. I’ll show examples of DSLs in several chapters.
6Groovy tests work for Java code.
The Spock testing tool, demonstrated in this chapter and extensively discussed in chapter 6 on testing, is a great way to test Java systems.
7Groovy build tools work on Java (and mixed) projects.
In chapter 5 on enhancing build processes, I’ll talk about AntBuilder, how to add Groovy to Maven builds, and Gradle.
8Groovy projects like Grails and Griffon make developing web and desktop applications easier.
The Grails project is a complete-stack, end-to-end framework for building web applications, based on Spring and Hibernate. Griffon brings the same convention- over-configuration ideas to desktop development. Grails is discussed in chapter 8 on databases and chapter 10 on web applications.
When looking at the sorts of problems Java developers typically encounter, this list will be a source of ideas for making implementations simpler, easier to read and understand, and faster to implement.
1.3Java use cases and how Groovy helps
The examples I’ve discussed so far are all code-level simplifications. They’re very helpful, but I can do more than that. Groovy developers work on the same sorts of problems
www.it-ebooks.info
16 |
CHAPTER 1 Why add Groovy to Java? |
that Java developers do, so many higher-level abstractions have been created to make addressing those problems easier.
In this book I’m also going to survey the various types of problems that Java developers face on a regular basis, from accessing and implementing web services to using object-relational mapping tools to improving your build process. In each case I’ll examine how adding Groovy can make your life easier as a developer.
Here’s a list of some of the areas I’ll discuss as we go along, and I’ll give you a brief idea of how Groovy will help. This will also provide a lightweight survey of the upcoming chapters.
1.3.1Spring framework support for Groovy
One of the most successful open source projects in the Java industry today is the Spring framework. It’s the Swiss Army chainsaw of projects; it’s pervasive throughout the Java world and has tools for practically every purpose.
No one is ever going to suggest rewriting Spring in Groovy. It works fine in Java as it is. Nor is there any need to “port” it to Groovy. As far as Spring is concerned, compiled Groovy classes are just another set of bytecodes. Groovy can use Spring as though it’s just another library.
The developers of Spring, however, are well aware of Groovy and built in special capabilities for working with it. Spring bean files can contain inline scripted Groovy beans. Spring also allows you to deploy Groovy source code, rather than compiled versions, as so-called refreshable beans. Spring periodically checks the source code of refreshable beans for changes and, if it finds any, rebuilds them and uses the updated versions. This is a very powerful capability, as chapter 7 on working with Spring will show.
Finally, the developers of the Grails project also created a class called BeanBuilder, which is used to script Spring beans in Groovy. That brings Groovy capabilities to Spring bean files much the way Gradle enhances XML build files.
1.3.2Simplified database access
Virtually all Java developers work with databases. Groovy has a special set of classes to make database integration easy, and I’ll review them in chapter 8 on databases. I also show an example of working with a MongoDB database through a Groovy library that wraps the corresponding Java API.
I’ll also borrow from the Grails world and discuss GORM, the Grails ObjectRelational Mapping tool, a DSL for configuring Hibernate. In fact, GORM has been refactored to work with a variety of persistence mechanisms, including NoSQL databases like MongoDB, Neo4J, Redis, and more.
1.3.3Building and accessing web services
Another area of active development today is in web services. Java developers work with both SOAP-based and RESTful services, the former involving auto-generated proxies and the latter using HTTP as much as possible. REST is covered in chapter 9, and
www.it-ebooks.info