Table Of ContentKenneth A. Kousen
FOREWORD BY Guillaume Laforge
M A N N I N G
Making Java Groovy
Making Java Groovy
KENNETH A. KOUSEN
MANNING
SHELTER ISLAND
For online information and ordering of this and other Manning books, please visit
www.manning.com. The publisher offers discounts on this book when ordered in quantity.
For more information, please contact
Special Sales Department
Manning Publications Co.
20 Baldwin Road
PO Box 261
Shelter Island, NY 11964
Email: orders@manning.com
©2014 by Manning Publications Co. All rights reserved.
No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in
any form or by means electronic, mechanical, photocopying, or otherwise, without prior written
permission of the publisher.
Photographs in this book were created by Martin Evans and Jordan Hochenbaum, unless
otherwise noted. Illustrations were created by Martin Evans, Joshua Noble, and Jordan
Hochenbaum. Fritzing (fritzing.org) was used to create some of the circuit diagrams.
Many of the designations used by manufacturers and sellers to distinguish their products are
claimed as trademarks. Where those designations appear in the book, and Manning
Publications was aware of a trademark claim, the designations have been printed in initial caps
or all caps.
Recognizing the importance of preserving what has been written, it is Manning’s policy to have
the books we publish printed on acid-free paper, and we exert our best efforts to that end.
Recognizing also our responsibility to conserve the resources of our planet, Manning books
are printed on paper that is at least 15 percent recycled and processed without the use of
elemental chlorine.
Manning Publications Co. Development editor: Cynthia Kane
20 Baldwin Road Copyeditor: Melinda Rankin
PO Box 261 Proofreader: Melody Dolab
Shelter Island, NY 11964 Typesetter: Dennis Dalinnik
Cover designer: Marija Tudor
ISBN: 9781935182948
Printed in the United States of America
1 2 3 4 5 6 7 8 9 10 – MAL – 19 18 17 16 15 14 13
To my father, Morton Kousen, MD,
who taught me everything I know about dedication,
persistence, and facing the future with a positive and
upbeat attitude, despite whatever pain and difficulties lay ahead.
He will always be my best example of what a man should be.
brief contents
PART 1 UP TO SPEED WITH GROOVY. ........................................1
1 ■ Why add Groovy to Java? 3
2 ■ Groovy by example 18
3 ■ Code-level integration 46
4 ■ Using Groovy features in Java 64
PART 2 GROOVY TOOLS..........................................................91
5 ■ Build processes 93
6 ■ Testing Groovy and Java projects 126
PART 3 GROOVY IN THE REAL WORLD....................................165
7 ■ The Spring framework 167
8 ■ Database access 199
9 ■ RESTful web services 227
10 ■ Building and testing web applications 257
vii
contents
foreword xv
preface xvii
acknowledgments xix
about this book xxii
about the cover illustration xxvi
PART 1 UP TO SPEED WITH GROOVY. ............................1
1 Why add Groovy to Java? 3
1.1 Issues with Java 4
Is static typing a bug or a feature? 5 ■ Methods must be in a class,
even if you don’t need or want one 7 ■ Java is overly verbose 10
Groovy makes testing Java much easier 11 ■ Groovy tools simplify
your build 13
1.2 Groovy features that help Java 14
1.3 Java use cases and how Groovy helps 15
Spring framework support for Groovy 16 ■ Simplified database
access 16 ■ Building and accessing web services 16
Web application enhancements 17
1.4 Summary 17
ix
Description:5.4 The Java approach, part 2: Maven 105 . I was going to make a DRY—Don't Repeat Yourself—joke here but eventually decided against it. 8 Bell, Craig Walls, Brian Sam-Bodden, Andy Painter, Paul Rayner, Daniel how to teach Groovy bothered me for some time, because I knew that some