using groovy & grails in your spring boot project

67
Use Groovy & Grails in your Spring Boot projects, don't be afraid! By Fátima Casaú @fatimacasau

Upload: spring-by-pivotal

Post on 15-Apr-2017

1.624 views

Category:

Technology


2 download

TRANSCRIPT

Page 1: Using Groovy & Grails in your Spring Boot Project

Use Groovy & Grails in your Spring Boot projects,

don't be afraid!By Fátima Casaú

@fatimacasau

Page 2: Using Groovy & Grails in your Spring Boot Project

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 2

@fatimacasau

Fátima Casaú Pérez

Software Engineer since 8 years ago

Java Architect & Scrum Master in Paradigma Tecnológico

Specialized in Groovy & Grails environments

Recently, Spring Boot world

Page 3: Using Groovy & Grails in your Spring Boot Project

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 3

What is Spring Boot?

What is Groovy?

Where could you use Groovy in your Spring Boot Projects?

● Gradle

● Tests

● Anywhere?

● Groovy Templates

● GORM

● GSP’s

Overview

Page 4: Using Groovy & Grails in your Spring Boot Project

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 4

Spring Boot

Page 5: Using Groovy & Grails in your Spring Boot Project

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 5

Standalone

Auto-configuration - CoC

No XML

Embedded Container and Database

Bootstrapping

Groovy!!

Run quickly - Spring Boot CLI

Spring Boot

Page 6: Using Groovy & Grails in your Spring Boot Project

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 6

Groovy

Page 7: Using Groovy & Grails in your Spring Boot Project

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 7

Dynamic language

Optionally typed

@TypeChecked & @CompileStatic

Java Platform

Easy & expressive syntax

Powerful features

closures, DSL, meta-programming, functional programming, scripting, ...

Groovy

Page 8: Using Groovy & Grails in your Spring Boot Project

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 8

Spring Boot application in a single tweet

DEMO...

Page 9: Using Groovy & Grails in your Spring Boot Project

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 9

First, an useful tool!

Page 10: Using Groovy & Grails in your Spring Boot Project

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 10

GVMGroovy enVironment

Manager Tool

Page 11: Using Groovy & Grails in your Spring Boot Project

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 11

GVM

Manage parallel versions

Multiple Software Development Kits

Groovy, Grails, Gradle, Spring Boot, Vert.x, Griffon,...

Unix Systems

Linux, Mac OSX, CyWin, Solaris,...

Bash

Curl & Unzip available

Page 12: Using Groovy & Grails in your Spring Boot Project

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 12

Actual candidates

GroovyGrailsGriffonGradleGroovyservLazybonesVert.xCrashGlideSpring BootAsciidoctorJ...

This list will grow beyond Groovy candidates.

Page 13: Using Groovy & Grails in your Spring Boot Project

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 13

How to use GVM and install SDK’s

To install GVM Tool

.bashrc

directory .gvm is created in home directory

$ curl -s get.gvmtool.net | bash

#THIS MUST BE AT THE END OF THE FILE FOR GVM TO WORK!!![[ -s "/home/fcasau/.gvm/bin/gvm-init.sh" ]] && source "/home/fcasau/.gvm/bin/gvm-init.sh"

Page 14: Using Groovy & Grails in your Spring Boot Project

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 14

How to use GVM and install SDK’s

Useful commands$ gvm help$ gvm install springboot$ gvm list springboot$ gvm install springboot 1.2.5.RELEASE$ gvm use grails 2.5.0$ gvm default groovy 2.4.0$ gvm current$ gvm current gradle$ gvm uninstall grails 2.4.4$ gvm broadcast

Page 15: Using Groovy & Grails in your Spring Boot Project

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 15

HelloWorld.groovy

1 @RestController2 class HelloWorld {3 4 @RequestMapping("/")5 String home(){6 "*** Hello World ***"7 }8 9 10 11 }

Page 16: Using Groovy & Grails in your Spring Boot Project

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 16

But... what happens

behind?

Page 17: Using Groovy & Grails in your Spring Boot Project

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 17

HelloWorld.groovy

1 2 3 4 5 6 7 8 @RestController9 class HelloWorld {10 11 @RequestMapping("/")12 String home(){13 "***************** Hello World *******************"14 }15 16 17 18 19 }

Page 18: Using Groovy & Grails in your Spring Boot Project

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 18

HelloWorld.groovy

1 @Grab(group='org.springframework.boot:spring-boot-starter-web')2 3 import org.springframework.boot.SpringApplication4 import org.springframework.web.bind.annotation.*5 import org.springframework.boot.autoconfigure.*6 7 @EnableAutoConfiguration(exclude = GroovyTemplateAutoConfiguration)8 @RestController9 class HelloWorld {10 11 @RequestMapping("/")12 String home(){13 "***************** Hello World *******************"14 }15 16 static void main(String[] args){17 SpringApplication.run HelloWorld, args18 }19 }

Page 19: Using Groovy & Grails in your Spring Boot Project

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 19

Executing...

Page 20: Using Groovy & Grails in your Spring Boot Project

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 20

Where could you use

groovy?

Page 21: Using Groovy & Grails in your Spring Boot Project

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 21

Gradle

Page 22: Using Groovy & Grails in your Spring Boot Project

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 22

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

Page 23: Using Groovy & Grails in your Spring Boot Project

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 23

Building a Spring Boot app with Gradle

DEMO...

Page 24: Using Groovy & Grails in your Spring Boot Project

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 24

Install Gradle and create a Spring Boot project

$ mkdir -p springOne2gx/src/main/groovy$ touch springOne2gx/src/main/groovy/Application.groovy$ cd springOne2gx$ touch build.gradle

Edit build.gradle and Application.groovy with the code of the next two slides

Page 25: Using Groovy & Grails in your Spring Boot Project

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 25

build.gradle

1 buildscript {2 repositories {3 maven { url "https://repo.spring.io/libs-release" }4 mavenLocal()5 mavenCentral()6 }7 dependencies {8 classpath("org.springframework.boot:spring-boot-gradle-plugin:1.2.5.RELEASE")9 }10 }11 12 apply plugin: 'groovy'13 apply plugin: 'idea'14 apply plugin: 'spring-boot'15 16 jar {17 baseName = 'springOne2GX'18 version = '0.1.0'19 }20 21 repositories {22 maven { url "https://repo.spring.io/libs-release" }23 mavenLocal()24 mavenCentral()25 }26 27 dependencies {28 compile("org.springframework.boot:spring-boot-starter-web")29 compile "org.codehaus.groovy:groovy-all:2.2.0"30 }

Page 26: Using Groovy & Grails in your Spring Boot Project

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 26

Application.groovy

1 import org.springframework.boot.SpringApplication2 import org.springframework.boot.autoconfigure.SpringBootApplication3 4 @SpringBootApplication5 class Application {6 7 static void main(String[] args) {8 9 SpringApplication.run Application, args10 }11 }

Page 27: Using Groovy & Grails in your Spring Boot Project

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 27

Executing...

Page 28: Using Groovy & Grails in your Spring Boot Project

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 28

Testing with Spock

Page 29: Using Groovy & Grails in your Spring Boot Project

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 29

Spock framework & specification

Expressive

Groovy DSL’s

Easy to read tests

Well documented

Powerful assertions

Reports

Testing with Spock

Page 30: Using Groovy & Grails in your Spring Boot Project

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 30

Testing with Spock

DEMO...

Page 31: Using Groovy & Grails in your Spring Boot Project

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 31

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 result9 result.status == HttpStatus.OK.value()10 !result.data.results.isEmpty()11 result.data.status == ‘OK’12 result.data.results[0].address_components.any{13 it.long_name == 'Madrid'14 }15 }

Page 32: Using Groovy & Grails in your Spring Boot Project

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 32

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 6 expect: “result & status when call the REST API”7 def result = rest.get(path: uri, query: [address:address])8 resultsIsEmpty == result.data.results.isEmpty()9 result.data.status == status10 11 where: “address takes different values with different results & status”12 address | resultsIsEmpty | status13 'Madrid'| false | 'OK'14 'abdkji'| true | 'ZERO_RESULTS'15 '186730'| false | 'ZERO_RESULTS' // This fails!16 17 }

Page 33: Using Groovy & Grails in your Spring Boot Project

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 33

Condition not satisfied:

result.data.status == status| | | | || | OK | ZERO_RESULTS| | false| | 11 differences (8% similarity)| | (---)O(K-------)| | (ZER)O(_RESULTS)| [results:[[address_components:[[long_name:186730, short_name:186730, types:[postal_code]], [long_name:Russia, short_name:RU, types:[country, political]]], formatted_address:Russia, 186730, geometry:[bounds:[northeast:[lat:61.84356, lng:30.2495827], southwest:[lat:61.4653021, lng:30.0576869]], location:[lat:61.67159349999999, lng:30.1745203], location_type:APPROXIMATE, viewport:[northeast:[lat:61.84356, lng:30.2495827], southwest:[lat:61.4653021, lng:30.0576869]]], place_id:ChIJt3ErZ3qEmUYRPW2d-LubWzA, types:[postal_code]]], status:OK]groovyx.net.http.HttpResponseDecorator@5f5f6fd6

Assertions

Page 34: Using Groovy & Grails in your Spring Boot Project

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 34

Reports

Page 35: Using Groovy & Grails in your Spring Boot Project

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 35

dependencies { testCompile("org.springframework.boot:spring-boot-starter-test") testCompile("org.springframework:spring-test") testRuntime("org.spockframework:spock-spring:0.7-groovy-2.0") { exclude group: 'org.spockframework', module: 'spock-core' } testCompile("org.spockframework:spock-core:0.7-groovy-2.0") { exclude group: 'org.codehaus.groovy', module: 'groovy-all' } … }

Dependencies

Page 36: Using Groovy & Grails in your Spring Boot Project

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 36

● Simple Spring Boot Application with Spock Test:● https://github.com/tomaslin/gs-spring-boot-spock

● Documentation: ● http://docs.spockframework.org

More information

Page 37: Using Groovy & Grails in your Spring Boot Project

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 37

Executing...

Page 38: Using Groovy & Grails in your Spring Boot Project

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 38

Anywhere!!

Page 39: Using Groovy & Grails in your Spring Boot Project

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 39

Anywhere

Mix Java & Groovy easily

More expressive, simple & flexible than Java

Less code → less errors → more productivity

Extension of JDK -> GDK

@CompileStatic @TypeChecked

Domain classes, Controllers, Repositories,...

Page 40: Using Groovy & Grails in your Spring Boot Project

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 40

Groovy domain, repository and controller

DEMO...

Page 41: Using Groovy & Grails in your Spring Boot Project

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 41

Customer.groovy

1 import javax.persistence.*2 3 @Entity4 public class Customer {5 6 @Id7 @GeneratedValue(strategy= GenerationType.AUTO)8 long id;9 String firstName;10 String lastName;11 12 @Override13 String toString() {14 "Customer[id=$id, firstName=$firstName, lastName=$lastName"15 }

Domain class

Page 42: Using Groovy & Grails in your Spring Boot Project

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 42

Repository

CustomerRepository.groovy

1 import com.paradigma.springOne2gx.Customer2 import org.springframework.data.repository.CrudRepository3 import org.springframework.data.rest.core.annotation.RepositoryRestResource4 5 import java.util.List6 7 @RepositoryRestResource(collectionResourceRel = "customers", path = "customer")8 public interface CustomerRepository extends CrudRepository<Customer, Long> {9 10 List<Customer> findByLastName(@Param(value = "lastName") String lastName)11 12 }

Page 43: Using Groovy & Grails in your Spring Boot Project

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 43

Controllers

CustomerController.groovy

1 import org.springframework.beans.factory.annotation.Autowired2 import org.springframework.stereotype.Controller3 import org.springframework.web.bind.annotation.*4 5 @Controller6 public class CustomerController {7 8 @Autowired9 CustomerRepository customerRepository10 11 @RequestMapping("/customer/{id}")12 @ResponseBody13 public Customer findById(@PathVariable Long id) {14 return customerRepository.findOne(id)15 }

Page 44: Using Groovy & Grails in your Spring Boot Project

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 44

Adding customers in Application.groovy

1 @SpringBootApplication2 class Application {3 4 static void main(String[] args) {5 6 ConfigurableApplicationContext context = SpringApplication.run(Application.class)7 8 CustomerRepository repository = context.getBean(CustomerRepository.class)9 10 // save a couple of customers11 [[firstName:"Rachel Karen", lastName:"Green"],[firstName:"Monica E.", lastName:"Geller"],12 [firstName:"Phoebe", lastName:"Buffay"],[firstName:"Joey", lastName:"Tribbiani"],13 [firstName:"Ross", lastName:"Geller"],[firstName:"Chandler Muriel", lastName:"Bing"]].each {14 repository.save(new Customer(it)) 15 }16 17 // print all customers18 def customers = repository.findAll()19 println "Customers found with findAll():"20 customers.each { println it }21 22 // find a customer by ID23 Customer customer = repository.findOne(1L)24 println "Customer found with findOne(1L): $customer"25 26 // find customers by last name27 def lastName = "Bing"28 customers = repository.findByLastName(lastName)29 println "Customer found with findByLastName('$lastName'):"30 customers.each { println it }

Page 45: Using Groovy & Grails in your Spring Boot Project

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 45

dependencies { compile("org.springframework.boot:spring-boot-starter-data-rest")

compile("org.springframework.boot:spring-boot-starter-data-jpa")compile("com.h2database:h2")

… }

Dependencies

More information & exampleshttps://spring.io/guides/gs/accessing-data-jpa/

https://spring.io/guides/gs/accessing-data-rest/

Page 46: Using Groovy & Grails in your Spring Boot Project

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 46

Executing...

Page 47: Using Groovy & Grails in your Spring Boot Project

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 47

Groovy Templates

Page 48: Using Groovy & Grails in your Spring Boot Project

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 48

Groovy Template Framework

Based MarkupBuilder

Groovy DSL’s

Render readable views

Replace variables easily

Layouts

Groovy Templates

Page 49: Using Groovy & Grails in your Spring Boot Project

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 49

Groovy templates

DEMO...

Page 50: Using Groovy & Grails in your Spring Boot Project

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 50

list.tpl

1 layout 'layouts/main.tpl',2 pageTitle: 'List customers',3 mainBody: contents {4 ul {5 customers.each { customer ->6 li {7 p("$customer.firstName $customer.lastName ")8 }9 }10 }11 div{12 a(href:"/", "Go to home")13 }14 }

Page 51: Using Groovy & Grails in your Spring Boot Project

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 51

dependencies { compile("org.springframework.boot:spring-boot-starter-groovy-templates") … }

https://github.com/spring-projects/spring-boot/tree/master/spring-boot-samples/spring-boot-sample-web-groovy-templates

Spring Projects Example

Dependencies

Page 52: Using Groovy & Grails in your Spring Boot Project

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 52

Executing...

Page 53: Using Groovy & Grails in your Spring Boot Project

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 53

GORM

Page 54: Using Groovy & Grails in your Spring Boot Project

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 54

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

Grails Object Relational Mapping

Page 55: Using Groovy & Grails in your Spring Boot Project

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 55

GORM in Spring Boot

DEMO...

Page 56: Using Groovy & Grails in your Spring Boot Project

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 56

Customer.groovy

1 import grails.persistence.*2 3 @Entity4 public class Customer {5 6 @Id7 @GeneratedValue(strategy= GenerationType.AUTO)8 long id;9 String firstName;10 String lastName;11 12 @Override13 String toString() {14 "Customer[id=$id, firstName=$firstName, lastName=$lastName"15 }

Page 57: Using Groovy & Grails in your Spring Boot Project

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 57

1 // lines in application main class2 3 [[firstName:"Rachel Karen", lastName:"Green"],4 [firstName:"Monica E.", lastName:"Geller"],5 [firstName:"Chandler Muriel", lastName:"Bing"]].each {6 new Customer(it).save()7 }8 9 def customers = Customer.list() // implicit method list()10 customers.each {11 println it12 }13 14 Customer customer = Customer.get(1L) // implicit method get(...)15 println "Customer found with get(1L): $customer"16 17 def lastName = "Bing"18 customers = Customer.findAllByLastName(lastName)19 // implicit methods findAllBy... findBy...

Page 58: Using Groovy & Grails in your Spring Boot Project

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 58

dependencies { compile("org.grails:gorm-hibernate4-spring-boot:1.1.0.RELEASE") compile("com.h2database:h2") … }

Dependencies

Page 59: Using Groovy & Grails in your Spring Boot Project

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 59

Executing...

Page 60: Using Groovy & Grails in your Spring Boot Project

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 60

GSP's

Page 61: Using Groovy & Grails in your Spring Boot Project

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 61

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

Groovy Server Pages

Page 62: Using Groovy & Grails in your Spring Boot Project

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 62

GSP's in Spring Boot

Some code...

Page 63: Using Groovy & Grails in your Spring Boot Project

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 63

<g:if test="${session.role == 'admin'}"> <%-- show administrative functions --%></g:if><g:else> <%-- show basic functions --%></g:else>

<g:each in="${[1,2,3]}" var="num"> <p>Number ${num}</p>

</g:each>

<g:findAll in="${books}" expr="it.author == 'Stephen King'"> <p>Title: ${it.title}</p>

</g:findAll>

<g:dateFormat format="dd-MM-yyyy" date="${new Date()}" />

<g:render template="bookTemplate" model="[book: myBook]" />

Page 64: Using Groovy & Grails in your Spring Boot Project

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 64

dependencies { compile("org.grails:grails-gsp-spring-boot:1.0.0") compile("org.grails:grails-web-gsp:2.5.0") compile("org.grails:grails-web-gsp-taglib:2.5.0") compile("org.grails:grails-web-jsp:2.5.0") … }

Dependencies

Page 65: Using Groovy & Grails in your Spring Boot Project

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 65

Conclusions

Page 66: Using Groovy & Grails in your Spring Boot Project

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 66

If you use Groovy…

Less code → Less errors → More productivity

GDK → More features → Cool utilities

Spock → Cool Tests

Super-vitaminized project!!

Why use Groovy?

Page 67: Using Groovy & Grails in your Spring Boot Project

Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 67

That's all

THANKS!!