Right Money Management App For Your Financial Goals
Use groovy & grails in your spring boot projects
1. La importancia de un buen título en presentaciones
Use Groovy & Grails in your Spring Boot
projects,
don't be afraid!
@fatimacasau
2. La importancia de un buen título en presentaciones
Fátima Casaú Pérez
Software Engineer for over 7 years ago
Java Architect & Scrum Master in Paradigma Tecnológico
Specialized in Groovy & Grails environments
Recently, Spring Boot world
@fatimacasau
Use Groovy & Grails in your Spring Boot projects, don't be afraid!
@fatimacasau
3. La importancia de un buen título en presentaciones
What is Spring Boot?
What is Groovy?
Where could you use Groovy in your Spring Boot Projects?
●
Gradle
●
Tests
●
Groovy Templates
●
Anywhere?
●
GORM
●
GSP’s
Overview
@fatimacasau
Use Groovy & Grails in your Spring Boot projects, don't be afraid!
4. La importancia de un buen título en presentaciones
Spring Boot
Use Groovy & Grails in your Spring Boot projects, don't be afraid!
5. La importancia de un buen título en presentaciones
Standalone
Auto-configuration - CoC
No XML
Embedded Container and Database
Bootstrapping
Groovy!!
Run quickly - Spring Boot CLI
projects.spring.io/spring-boot
Spring Boot
@fatimacasau
Use Groovy & Grails in your Spring Boot projects, don't be afraid!
6. La importancia de un buen título en presentaciones
Spring Boot application
in a single tweet
DEMO...
7. La importancia de un buen título en presentaciones
GVM
gvmtool.net
> gvm install springboot
@fatimacasau
Use Groovy & Grails in your Spring Boot projects, don't be afraid!
8. La importancia de un buen título en presentaciones
@fatimacasau
Use Groovy & Grails in your Spring Boot projects, don't be afraid!
HelloWorld.groovy
1 @Controller
2 class ThisWillActuallyRun {
3 @RequestMapping("/")
4 @ResponseBody
5 String home() {
6 "Hello World!"
7 }
8 }
Spring Boot in a single Tweet
9. La importancia de un buen título en presentaciones
@fatimacasau
Use Groovy & Grails in your Spring Boot projects, don't be afraid!
apply plugin: 'springboot'
dependencies {
compile("org.springframework.boot:springbootstarterweb")
…
}
https://github.com/spring-projects/spring-boot/tree/master/spring-boot-samples
10. La importancia de un buen título en presentaciones
Groovy
Use Groovy & Grails in your Spring Boot projects, don't be afraid!
11. La importancia de un buen título en presentaciones
Dynamic language
Optionally typed
@TypeChecked & @CompileStatic
Java Platform
Easy & expressive syntax
Powerful features
closures, DSL, meta-programming, functional programming, scripting, ...
Groovy
@fatimacasau
Use Groovy & Grails in your Spring Boot projects, don't be afraid!
12. La importancia de un buen título en presentaciones
@fatimacasau
Use Groovy & Grails in your Spring Boot projects, don't be afraid!
apply plugin: 'groovy'
dependencies {
compile("org.codehaus.groovy:groovyall:2.2.0")
…
}
13. La importancia de un buen título en presentaciones
Where could you use
Groovy?
Use Groovy & Grails in your Spring Boot projects, don't be afraid!
14. La importancia de un buen título en presentaciones
Gradle
Use Groovy & Grails in your Spring Boot projects, don't be afraid!
15. La importancia de un buen título en presentaciones
Powerful build tool
Support multi-project
Dependency management (based on Apache Ivy)
Support and Integration with Maven & Ivy repositories
Based on Groovy DSL
Build by convention
Ant tasks
Gradle
@fatimacasau
Use Groovy & Grails in your Spring Boot projects, don't be afraid!
16. La importancia de un buen título en presentaciones
Building a Spring Boot
application with Gradle
DEMO...
17. La importancia de un buen título en presentaciones
@fatimacasau
Use Groovy & Grails in your Spring Boot projects, don't be afraid!
GVM
> gvm install gradle
> gradle build
> gradle tasks
18. La importancia de un buen título en presentaciones
@fatimacasau
Use Groovy & Grails in your Spring Boot projects, don't be afraid!
build.gradle
1 buildscript {
2 repositories {
3 mavenCentral()
4 }
5 dependencies {
6 classpath("org.springframework.boot:springbootgradleplugin:1.2.2.RELEASE")
7 }
8 }
9
10 apply plugin: 'groovy'
11 apply plugin: 'idea'
12 apply plugin: 'springboot'
13
14 jar {
15 baseName = 'helloworld'
16 version = '0.1.0'
17 }
18
19 repositories {
20 mavenCentral()
21 }
22
23 dependencies {
24 compile("org.springframework.boot:springbootstarterweb")
25 }
19. La importancia de un buen título en presentaciones
Testing with Spock
Use Groovy & Grails in your Spring Boot projects, don't be afraid!
20. La importancia de un buen título en presentaciones
Spock framework & specification
Expressive
Groovy DSL’s
Easy to read tests
Well documented
Powerful assertions
Testing with Spock
@fatimacasau
Use Groovy & Grails in your Spring Boot projects, don't be afraid!
21. La importancia de un buen título en presentaciones
Testing with Spock
DEMO...
22. La importancia de un buen título en presentaciones
@fatimacasau
Use Groovy & Grails in your Spring Boot projects, don't be afraid!
GoogleSpec.groovy
1 void "test Google Maps API where address is ‘Madrid’"(){
2 setup: “Google Maps API Host & Uri”
3 def rest = new RESTClient("https://maps.googleapis.com")
4 def uri = "/maps/api/geocode/json"
5 when: “Call the API with address = ‘madrid’”
6 def result = rest.get(path: uri, query: [address:'madrid'])
7 then: “HttpStatus is OK, return a list of results and field status = OK”
8 result
9 result.status == HttpStatus.OK.value()
10 !result.data.results.isEmpty()
11 result.data.status == ‘OK’
12 result.data.toString().contains('Madrid')
13 }
23. La importancia de un buen título en presentaciones
@fatimacasau
Use Groovy & Grails in your Spring Boot projects, don't be afraid!
GoogleSpec.groovy
1 void "test Google Maps API with different values"(){
2 setup: “Google Maps API Host & Uri”
3 def rest = new RESTClient("https://maps.googleapis.com")
4 def uri = "/maps/api/geocode/json"
5 expect: “result & status when call the REST API”
6 def result = rest.get(path: uri, query: [address:address])
7 resultsIsEmpty == result.data.results.isEmpty()
8 result.data.status == status
9 where: “address takes different values with different results & status”
10 address | resultsIsEmpty | status
11 'Madrid'| false | 'OK'
12 'abdkji'| true | 'ZERO_RESULTS'
13 '186730'| false | 'ZERO_RESULTS' // This fails!
14
15 }
24. La importancia de un buen título en presentaciones
@fatimacasau
Use Groovy & Grails in your Spring Boot projects, don't be afraid!
Assertion failed:
assert resultsIsEmpty == result.data.results.isEmpty()
| | | | | |
false false | | true
| | [...]
| |
| ...
...
docs.spockframework
.org
25. La importancia de un buen título en presentaciones
@fatimacasau
Use Groovy & Grails in your Spring Boot projects, don't be afraid!
dependencies {
testCompile("org.springframework.boot:springbootstartertest")
testCompile("org.springframework:springtest")
testRuntime("org.spockframework:spockspring:0.7groovy2.0") {
exclude group: 'org.spockframework', module: 'spockcore'
}
testCompile("org.spockframework:spockcore:0.7groovy2.0") {
exclude group: 'org.codehaus.groovy', module: 'groovyall'
}
…
}
https://github.com/tomaslin/gs-spring-boot-spock
26. La importancia de un buen título en presentaciones
Groovy templates
Use Groovy & Grails in your Spring Boot projects, don't be afraid!
27. La importancia de un buen título en presentaciones
Groovy Template Framework
Based MarkupBuilder
Groovy DSL’s
Render readable views
Replace variables easily
Groovy templates
@fatimacasau
Use Groovy & Grails in your Spring Boot projects, don't be afraid!
28. La importancia de un buen título en presentaciones
Groovy Templates
DEMO...
29. La importancia de un buen título en presentaciones
@fatimacasau
Use Groovy & Grails in your Spring Boot projects, don't be afraid!
1 ul {
2 people.each { p >
3 li(p.name)
4 }
5 }
With the following model
6 def model = [people: [
7 new Person(name:'Bob'),
8 new Person(name:'Alice')
9 ]]
Renders the following
10 <ul><li>Bob</li><li>Alice</li></ul>
30. La importancia de un buen título en presentaciones
@fatimacasau
Use Groovy & Grails in your Spring Boot projects, don't be afraid!
dependencies {
compile("org.springframework.boot:springbootstartergroovytemplates")
…
}
https://github.com/spring-projects/spring-boot/tree/master/spring-boot-samples/spring-
boot-sample-web-groovy-templates
31. La importancia de un buen título en presentaciones
Anywhere!
Use Groovy & Grails in your Spring Boot projects, don't be afraid!
32. La importancia de un buen título en presentaciones
Anywhere
Mix Java & Groovy easily
More expressive, simple & flexible than Java
Extension of JDK -> GDK
@CompileStatic @TypeChecked
Controllers, Services, Model,...
@fatimacasau
Use Groovy & Grails in your Spring Boot projects, don't be afraid!
33. La importancia de un buen título en presentaciones
Groovy Controller
DEMO...
34. La importancia de un buen título en presentaciones
@fatimacasau
Use Groovy & Grails in your Spring Boot projects, don't be afraid!
HelloWorld.groovy
1
2 @Controller
3 class ThisWillActuallyRun {
4 @RequestMapping("/")
5 @ResponseBody
6 String home() {
7 "Hello World!"
8 }
9 }
@groovy.transform.CompileStatic
35. La importancia de un buen título en presentaciones
GORM
Use Groovy & Grails in your Spring Boot projects, don't be afraid!
36. La importancia de un buen título en presentaciones
Automatic mapping for entities
Dynamic finders, criterias, persistence methods, validation,
mappings, constraints…
Expressive and simple code
implicit getters & setters
implicit constructors
implicit primary key
For Hibernate
For MongoDB
GORM: Grails Object Relational Mapping
@fatimacasau
Use Groovy & Grails in your Spring Boot projects, don't be afraid!
37. La importancia de un buen título en presentaciones
GORM for Hibernate
DEMO...
38. La importancia de un buen título en presentaciones
@fatimacasau
Use Groovy & Grails in your Spring Boot projects, don't be afraid!
Customer.groovy
1 @Entity
2 public class Customer {
3
4 String firstName;
5 String lastName;
6
7 static constraints = {
8 firstName blank:false
9 lastName blank:false
10 }
11 static mapping = {
12 firstName column: 'first_name'
13 lastName column: 'last_name'
14 }
15 }
39. La importancia de un buen título en presentaciones
@fatimacasa
uUse Groovy & Grails in your Spring Boot projects, don't be afraid!
1 [[firstName:"Jack", lastName:"Bauer"],
2 [firstName:"Michelle", lastName:"Dessler"]].each {
3 new Customer(it).save()
4 }
5
6 def customers = Customer.findAll()
7
8 customers.each {
9 println it
10 }
11
12 def customer = Customer.get(1L)
13
14 customers = Customer.findByLastName("Bauer")
15 customers.each {println it}
40. La importancia de un buen título en presentaciones
@fatimacasau
Use Groovy & Grails in your Spring Boot projects, don't be afraid!
dependencies {
compile("org.grails:gormhibernate4springboot:1.1.0.RELEASE")
compile("com.h2database:h2")
…
}
https://github.com/fatimacasau/spring-boot-talk/blob/spring-boot-groovy-gorm
41. La importancia de un buen título en presentaciones
GSP's
Use Groovy & Grails in your Spring Boot projects, don't be afraid!
42. La importancia de un buen título en presentaciones
Groovy Server Pages is used by Grails
Large list of useful Tag Libraries
Easy to define new Tags
Not only views
Layouts & Templates
Reuse Code
GSP's: Groovy Server Pages
@fatimacasau
Use Groovy & Grails in your Spring Boot projects, don't be afraid!
43. La importancia de un buen título en presentaciones
GSP's in Spring Boot
DEMO...
44. La importancia de un buen título en presentaciones
@fatimacasau
Use Groovy & Grails in your Spring Boot projects, don't be afraid!
1 <g:if test="${session.role == 'admin'}">
2 <% show administrative functions %>
3 </g:if>
4 <g:else>
5 <% show basic functions %>
6 </g:else>
___________________________________________________________________
1 <g:each in="${[1,2,3]}" var="num">
2 <p>Number ${num}</p>
3 </g:each>
___________________________________________________________________
1 <g:findAll in="${books}" expr="it.author == 'Stephen King'">
2 <p>Title: ${it.title}</p>
3 </g:findAll>
___________________________________________________________________
1 <g:dateFormat format="ddMMyyyy" date="${new Date()}" />
___________________________________________________________________
1 <g:render template="bookTemplate" model="[book: myBook]" />
45. La importancia de un buen título en presentaciones
@fatimacasau
Use Groovy & Grails in your Spring Boot projects, don't be afraid!
dependencies {
compile("org.grails:grailsgspspringboot:1.0.0")
compile("org.grails:grailswebgsp:2.5.0")
compile("org.grails:grailswebgsptaglib:2.5.0")
compile("org.grails:grailswebjsp:2.5.0")
…
}
https://github.com/grails/grails-boot/tree/master/sample-apps/gsp/gsp-example
46. La importancia de un buen título en presentaciones
Conclusions
Use Groovy & Grails in your Spring Boot projects, don't be afraid!
47. La importancia de un buen título en presentaciones
If you use Groovy…
Less code
More features
Cool Tests
Cool utilities
Why not? Please try to use Groovy!
@fatimacasau
Use Groovy & Grails in your Spring Boot projects, don't be afraid!
49. La importancia de un buen título en presentaciones
MVC Spring based Apps
Convention Over Configuration
Bootstrapping
Groovy
GORM
GSP’s ...
It's sounds like Grails!
@fatimacasau
Use Groovy & Grails in your Spring Boot projects, don't be afraid!
50. La importancia de un buen título en presentaciones
Why do you not use Grails?
Use Groovy & Grails in your Spring Boot projects, don't be afraid!
51. La importancia de un buen título en presentaciones
Thanks!!
@fatimacasau
52. La importancia de un buen título en presentaciones
EXAMPLE
http://github.com/fatimacasau/spring-boot-talk
Simple API Rest with Spring Boot, Groovy and GORM
@fatimacasau
Use Groovy & Grails in your Spring Boot projects, don't be afraid!
53. La importancia de un buen título en presentaciones
We are hiring!
JEE, Python, PHP, MongoDB, Cassandra, Big Data, Scala, NoSQL,
AngularJS, Javascript, iOS, Android, HTML, CSS3… and Commitment,
Ping Pong, Arcade…
SEND US YOUR CV
Notes de l'éditeur
My name is Fatima Casau
I’m a software engineer since seven years ago and currently, I work as a Java Architect and Scrum Master in Paradigma Tecnologico
Since 6 years ago, I’m specialized in Groovy & Grails technologies and recently, I&apos;m working with Spring Boot. For this, I&apos;m here and I want to tell you my experience about this
Well...
In short, What are we going to see in this talk?
I’m going to talk about Spring Boot and Groovy and where you could use Groovy in your spring boot projects
We go to see...
What is Spring Boot
What is Groovy
And, how or where use both. For example
Using Gradle
Developing tests with groovy and the Spock framework
Using Groovy Templates
Or why not?, Anywhere in your projects
Also, We will see how to use GORM and GSP’s in a Spring Boot project, outside Grails
In First Place, What is Spring Boot?
Question: How many people know Spring Boot?
Question: How many people are using Spring Boot currently?
(If many people -&gt; As many of you know...
… Spring Boot is a new project from Spring that allows you to develop standalone Spring MVC applications easily.
Probably, You think about the many configurations there are in Spring projects, but, Spring Boot, follows Convention Over Configuration allowing Autoconfiguration and configuration with annotations, eliminating the necessity of use XML configuration files.
In other hand, Spring Boot allows you to start and build production applications quickly, because
provides an embedded container and database according to the dependencies specified in the build system, so, you don&apos;t need to configure this to start to work.
Additionally, we can use Groovy, and, of course, all of its features, only including a dependency.
Finally, we can run an application quickly thanks to Spring Boot CLI Tool
Of course, you can find examples and documentation about all of this on the Spring Boot website in projects (dot) spring (dot) io (slash) spring-boot
We go to see a little but powerful example:
We can develop an application that can be contained in a single tweet and execute it quickly with Spring Boot CLI
First, I want to talk about the GVM Tool
GVM is a Tool for managing versions of multiple Software Development Kits
To use it, you can download in its website gvmtool.net
Once you have downloaded, you can see witch framework are available only typing &apos;gvm&apos;
you can download Spring Boot with the GVM Tool
Once you have installed GVM tool, only you need to do is execute in a terminal gvm install springboot and this will install the latest version of spring boot
Here we have the example: a groovy script with:
an annotation (at) Controller that means this is a Controller component
two more annotations, to determine the URI and response of the controller
and finally, a method that returns the message “Hello World”
To execute this, we open a terminal and go to the path that contains the groovy script and type spring run helloWorld.groovy
(By command line)
go to workspaces/greach14
spring run helloWorld.groovy
Later, go to Chrome and type localhost:8080 and see the result
It&apos;s only you need to do to start an example
In second place, The next lead actor in this talk, of course, is Groovy!
Question: How many people know Groovy?
Question: How many people are using Groovy currently?
What is Groovy?
Groovy is a dynamic language,
optionally typed with static-typing and static compilation capabilities for the Java platform
Thanks to an easy and expressive syntax it’s easy to learn for java developers.
Furthermore, it integrates easily with any Java program, delivering powerful features, including closures, runtime and compile-time meta-programming, functional programming, scripting, powerful tests...and so on
So..., if we combine the power of Spring Boot with the features of Groovy
we obtain a couple of powerful tools that allows to developers work
in an easy and more productive, interesting and amazing way
Then....
Where could you introduce Groovy in your spring boot applications? In short, anywhere!, but we go to see the different sites in the project where you could use Groovy bit by bit
The first option is use Groovy in the process to build the project using Gradle
With Spring Boot we can build our projects with Maven, but also, we can use Gradle
With Maven, we have to manage a static xml file, the pom.xml file, but with Gradle we have a flexible and modular groovy file that it&apos;s more easy to read thanks to a friendly syntax based on Groovy DSL’s and a plugin model
With Gradle, we have a powerful building tool with many features such as support for multi-project, dependency management based on Apache Ivy, with support and integration with Maven and Ivy repositories.. And also, we have a large list of tasks based on Ant to manage our application
See you how to use
Like with Spring Boot, we can download and manage our versions of Gradle with GVM Tool
Later, we only need to create a build.gradle file with some dependencies and execute the command gradle build to compile, test and assemble the code into a jar file.
If we execute gradle ‘tasks’ we can see a list of available for our project.
This is an example of build.gradle file.
We can see some plugins such as groovy, idea or spring boot, of course.
Also, we have got the configurations about the jar file, the name and the version of our project
Below, the repositories configuration and the dependencies.
If you include some plugin more and execute the ‘tasks’ command again, you will can see new tasks in the list, the tasks are related with the dependencies available in the project
Other site where use Groovy is implementing tests using the framework Spock.
This is my favorite section
I like to implement tests with groovy and Spock framework
Why?
Spock is not only a test framework, Spock is a test specification for Groovy and Java projects
With an expressive language, its also based on Groovy DSL’s and this makes our tests become easy to read and well documented.
Moreover, when an assertion failed, the exit is very intuitive and easy to read as well.
Let me to show some examples of simple spock tests and how to use its test blocks
For example,
With Spock we can define test with descriptive signatures.
We can organize the code in intuitive blocks such as:
“given” or “setup” to setup initial variables, for example,
“when” to execute an action
and “then” to test the result. In this block we needn’t asserts, only test if a sentence is true or not by the use of groovy truth
In the other hand we have got two blocks more, ‘expect’ and ‘where’. With this blocks we can test different combinations of data in only one test and later, we will see what iterations it has failed and what not easily
Here, we can view the way to show the assertions failed. It’s very easy read the failure
In addition to these, we have more features with diferents tags and utilities. Please visit the documentation and test the examples
I think that this is a good point to start with Groovy and know the features and the simplicity of the language
Another possibility is the use of Groovy templates
Groovy Template Framework is based on MarkupBuild that permits a pretty and easy way to write readable Groovy Views using Groovy DSL&apos;s
Also, provide an easy way to replace variables in the view
See you an example
In this block, we have an ‘ul’ block where a people list is iterated and for each person, its name is printed in a li element. We can pass a list and iterate it in the view easily
If we return this view and, in the model, we send this list of people, we obtain the result printed in the line below
Right?
Well...,
Really, You can use Groovy anywhere in your spring boot project
Yes! Anywhere!
We can use Groovy in controllers, services, domain classes…
If we need, we can combine Java & Groovy easily, that is, if we need have classes in Java & in other hand, classes in Groovy, it is possible.
And, you must to know that the most important features of use groovy over java is that our applications will be very simple, expressive and flexible, will have less code and, if there are less code, there will be less errors. This is a fact!
Furthermore, with Groovy we have many features available than with Java, because Groovy provides the GDK as an extension of JDK with many more features about the use of Strings, collections, control structures, closures… and so on.
Finally, you could think about compilation in runtime or dynamic typing, but, if you are worry about this, you must to know that you can use Static Compilation or Static Typing with the annotations (at)CompileStatic and (at)TypeChecked in your classes.
Here, I won&apos;t to show anything different, only, the same controller in the beginning of the talk but with the use of CompileStatic annotation
Only, you need to add the annotation (at)CompileStatic in the definition of the class
This code should already run as fast as Java.
The use of GORM with Spring Boot
GORM is the Grails Object Relational Mapping
Since Grails 2.4, It is possible to use GORM outside of Grails, and this permits access relational database or MongoDB with GORM in a Spring Application including the correct dependency in the project.
With GORM we can map the entities automatically, we have dynamic finders, criterias, persistence methods, validations… and more. We don’t need implements getters & setters methods, constructors or specify the primary key.
With this features, our code is even simple
See some code...
This is a domain class implemented with Groovy and using GORM. The annotation @Entity indicates that we are using GORM.
We can specify some constraints easily, or some mapping options.
Now, see how to use a domain class with GORM.
For example. We can instance new object of the class with a map in the constructor and we have not defined this constructor
In other hand, We have dynamic finders without implement it, findAll, get, findBy… any attributes… and many more
As GORM, we can use GSP’s (Groovy Server Pages) outside of Grails.
With GSP’s we have a large list of Tag libraries that we can use to make our views more simple.
Also, we can create easily our own tags or we can define layouts, templates and reuse the code easily
See some examples of predefined tags:
conditional tags as g:if and g:else
iterator tags as g:each or using a finder that iterates a list with a condition
g:dateFormat to format dates in view easily
g:render to render templates sending a model
and many more!
Also, remind that you can implements your own tags easily
well… after all of this…
What are the conclusions? (Almost my conclusions)
If you use Groovy… as I’ve said before, your application has less lines of code, so, less errors, if there are errors, you can find them easily so, you will be more productive
Remind that with Groovy you can get more features for your project, you can implement cool test and you can use cool utilities, this is: you have a super-vitaminized Project!
Then, Why not use Groovy in your Spring Boot project? Please, use Groovy! Try to introduce bit to bit in the application. Begin with Spock tests, later, introduce groovy in the code of your services, and so on.
But, one moment...
We have talked about MVC Spring Based Apps
Convention Over Configuration
Application Bootstrapping
Groovy Language
GORM
GSP’s….
This sounds like Grails!
And also, Grails 3.0 is built on Spring Boot!!
So, I want to leave you with a question,... Why not use Grails instead of Spring Boot?
The next lead actor in this talk is Groovy!
Question: How many people know Groovy?
Question: How many people are using Groovy currently?
One more thing.
In Paradigma, we are hiring for people specialized in many technologies as you can see, so, if your are interested or you know somebody could be interested, please, send us your CV
&lt;number&gt;