2. About me
Fátima Casaú Pérez / @fatimacasau
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 /{PARADIGMA TECNOLÓGICO
Microservices, Grails or Spring Boot?
Picture: Cueva La Mora, Embalse García de Sola, Talarrubias (Badajoz)
3. Overview
Spring Boot, Groovy
GVM Tool
Example 1: Implement simple REST API
●
Spring Boot, Gradle, Groovy, Spring Data JPA
Example 2: Implement simple REST API
●
Grails
Other important Tips
Conclusions
@fatimacasau /{PARADIGMA TECNOLÓGICO
Microservices, Grails or Spring Boot?
5. Spring Boot
Standalone
Auto-configuration - CoC
No XML
Embedded Container and Database
Bootstrapping
Groovy!!
Run quickly - Spring Boot CLI
@fatimacasau /{PARADIGMA TECNOLÓGICO
Microservices, Grails or Spring Boot?
projects.spring.io/spring-boot
9. GVM
Manage parallel versions
Multiple Software Development Kits
– Groovy, Grails, Gradle, Spring Boot, Vert.x, Griffon,...
Unix Sistems
– Linux, Mac OSX, CyWin, Solaris,..
Bash
Curl & Unzip available
@fatimacasau /{PARADIGMA TECNOLÓGICO
Microservices, Grails or Spring Boot?
gvmtool.net
10. GVM – How to ...
@fatimacasau /{PARADIGMA TECNOLÓGICO
Microservices, Grails or Spring Boot?
$ gvm help
$ curl s get.gvmtool.net | bash
$ gvm install groovy
$ gvm install groovy 2.4.0
$ gvm use grails 2.5.0
$ gvm current
$ gvm current gradle
$ gvm list springboot
$ gvm default groovy 2.4.0
$ gvm uninstall grails 2.4.4
11. Spring Boot App in a single Tweet with Groovy
HelloWorld.groovy
@Controller
class ThisWillActuallyRun {
@RequestMapping("/")
@ResponseBody
String home() {
"Hello World!"
}
}
@fatimacasau /{PARADIGMA TECNOLÓGICO
Microservices, Grails or Spring Boot?
Spring Boot in a single tweet
12. Run the Spring Boot Groovy script
@fatimacasau /{PARADIGMA TECNOLÓGICO
Microservices, Grails or Spring Boot?
$ spring run helloWorld.groovy
. ____ _ __ _ _
/ / ___'_ __ _ _(_)_ __ __ _
( ( )___ | '_ | '_| | '_ / _` |
/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |___, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v1.2.2.RELEASE)
...
... : Root WebApplicationContext: initialization completed
in 4024 ms
...
... : Started application in 8.042 seconds (JVM running
for 15.542)
$ gvm current springboot
Using springboot version 1.2.2.RELEASE
13. Spring BootSpring Boot
Implement Simple REST APIImplement Simple REST API
@fatimacasau /{PARADIGMA TECNOLÓGICO
Microservices, Grails or Spring Boot?
Example 1:Example 1:
14. How to...
Create project with Gradle
Bootstraping
Create an entity
Create a repository
Create custom controller
Configure properties
Testing
Execute
Deploy
@fatimacasau /{PARADIGMA TECNOLÓGICO
Microservices, Grails or Spring Boot?
16. Gradle
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
@fatimacasau /{PARADIGMA TECNOLÓGICO
Microservices, Grails or Spring Boot?
44. Execute (packaging Jar or War)
Using Gradle
Using Maven
In place
Create a war file
Gradle: some options in build.gradle
Maven:
$ gradle build && java jar build/libs/springbootexample0.1.0.jar
$ mvn package && java jar target/springbootexample0.1.0.jar
$ gradle bootRun
$ mvn springboot:run
apply plugin: 'war'
war {
baseName = 'myapp'
version = '0.5.0'
}
<packaging>war</packaging>
48. GrailsGrails
Implement Simple REST APIImplement Simple REST API
@fatimacasau /{PARADIGMA TECNOLÓGICO
Microservices, Grails or Spring Boot?
Example 2:Example 2:
50. Grails
Powerful web application Framework
Java Platform (JVM)
Groovy-based
CoC (Convention over Configuration)
ORM/NoSql
Plugins
DSL's
Grails 3 built on Spring Boot
@fatimacasau /{PARADIGMA TECNOLÓGICO
Microservices, Grails or Spring Boot?
51. How to...
Create project → Bootstraping
Create an Entity
RESTful API
Create REST Controller
Create REST Custom Controller
Testing
Configuration
Execute
Deploy
@fatimacasau /{PARADIGMA TECNOLÓGICO
Microservices, Grails or Spring Boot?
53. Create Project - Bootstraping
@fatimacasau /{PARADIGMA TECNOLÓGICO
Microservices, Grails or Spring Boot?
$ grails createapp grails3apirest
build.gradle file by default
Main class Application.groovy by default
application.yml configuration by default
Layout & some view by default
i18n files by default
log configuration by default
Url mappings by default
54. Example HelloWorld
@fatimacasau /{PARADIGMA TECNOLÓGICO
Microservices, Grails or Spring Boot?
$ grails
| Enter a command name to run. Use TAB for completion:
grails> createcontroller HelloWorld
| Created grailsapp/controllers/grails3/rest/sample/HelloWorldController.groovy
| Created src/test/groovy/grails3/rest/sample/HelloWorldControllerSpec.groovy
grails>
Grails provides an interactive console
HelloWorldController.groovy class is created
Default controllers directory
Default packaging
package grails3.rest.sample
class HelloWorldController {
def index() { }
}
{ “Hello World" }
grails> runapp
58. Create domain class
@fatimacasau /{PARADIGMA TECNOLÓGICO
Microservices, Grails or Spring Boot?
grails> createdomainclass Person
| Created grailsapp/domain/grails3/rest/sample/Person.groovy
| Created src/test/groovy/grails3/rest/sample/PersonSpec.groov
grails>
Person.groovy persistence class is created
Default domain directory
package grails3.rest.sample
class Person {
static constraints = {
}
}
59. Person.groovy
@fatimacasau /{PARADIGMA TECNOLÓGICO
Microservices, Grails or Spring Boot?
1 import grails.rest.Resource
2
3
4 class Person {
5
6 String firstName
7 String lastName
8 Integer age
9
10 static constraints = {
11 lastName maxSize: 20
12 firstName maxSize: 50
13 age nullable: true
14 }
15 } 16
60. Person.groovy
@fatimacasau /{PARADIGMA TECNOLÓGICO
Microservices, Grails or Spring Boot?
1 import grails.rest.Resource
2
3 @Resource(uri='/people')
4 class Person {
5
6 String firstName
7 String lastName
8 Integer age
9
10 static constraints = {
11 lastName maxSize: 20
12 firstName maxSize: 50
13 age nullable: true
14 }
15 } 16
61. Person.groovy
@fatimacasau /{PARADIGMA TECNOLÓGICO
Microservices, Grails or Spring Boot?
1 import grails.rest.Resource
2
3 @Resource(uri='/people', formats=['json', 'xml'])
4 class Person {
5
6 String firstName
7 String lastName
8 Integer age
9
10 static constraints = {
11 lastName maxSize: 20
12 firstName maxSize: 50
13 age nullable: true
14 }
15 } 16
62. Person.groovy
@fatimacasau /{PARADIGMA TECNOLÓGICO
Microservices, Grails or Spring Boot?
1 import grails.rest.Resource
2
3 @Resource(uri='/people', readOnly=true)
4 class Person {
5
6 String firstName
7 String lastName
8 Integer age
9
10 static constraints = {
11 lastName maxSize: 20
12 firstName maxSize: 50
13 age nullable: true
14 }
15 } 16
RESTful API only with one annotation
Dynamic finders, where queries, criterias, … without repositories
64. PersonController.groovy
@fatimacasau /{PARADIGMA TECNOLÓGICO
Microservices, Grails or Spring Boot?
1 import grails.rest.RestfulController
2
3 class PersonController extends RestfulController{
4
5 static responseFormats = ['json', 'xml']
6
7 PersonController(){
8 super(Person)
9 }
10 } 11
RESTful Url mapping to controller by
convention
in URLMappings.groovy
"/people"(resources: 'person')
HTTP Method URI Grails Action
GET /people index
GET /people/create create
POST /people save
GET /people/${id} show
GET /people/${id}/edit edit
PUT /people/${id} update
DELETE /people/${id} delete
65. PersonController.groovy
@fatimacasau /{PARADIGMA TECNOLÓGICO
Microservices, Grails or Spring Boot?
1 @Transactional(readOnly = true)
2 class PersonController {
3 static responseFormats = ['json', 'xml']
4
5 def index(Integer max) {
6 params.max = Math.min(max ?: 10, 100)
7 respond Person.list(params), model:[personCount:Person.count()]
8 }
9
10 def show(Person person) {
11 if(person) {
12 render status:404
13 }
14 else {
15 respond person
16 }
17 }
18
19 }
HTTP Method URI Grails Action
GET /people index
GET /people/create create
POST /people save
GET /people/${id} show
GET /people/${id}/edit edit
PUT /people/${id} update
DELETE /people/${id} delete
RESTful Url mapping to controller by
convention
in URLMappings.groovy
"/people"(resources: 'person')
67. Testing
Spock Framework integrated by default
Similar use as Spring Boot
– Well integrated (more easy, more features)
– Test Mixins for Unit test. Support for:
●
Domains and constraints
●
Controllers and filters
●
Tag libraries
●
Url mappings
●
Metaprogramming
– Integration Tests
– Functional Tests (GEB integrated by default)
@fatimacasau /{PARADIGMA TECNOLÓGICO
Microservices, Grails or Spring Boot?
See
Documentation!
69. Configuration
Similar as Spring Boot
Built configuration: build.gradle file
Runtime configuration: grailsapp/conf/application.yml by default
Grails 2.0-style: grailsapp/conf/application.groovy
Access: grailsApplication.config.getProperty('foo.bar.hello')
Spring @Value annotation
@fatimacasau /{PARADIGMA TECNOLÓGICO
Microservices, Grails or Spring Boot?
70. Configuration per Environments
Easy use of configuration by environments
Commands by environment
– Default environments: grails [environment] [command name]
– Custom environments: grails Dgrails.env=[customenvname] [commandname]
– Programmatic env. detection, per env. bootstraping, generic per env. execution
@fatimacasau /{PARADIGMA TECNOLÓGICO
Microservices, Grails or Spring Boot?
# Default environments
environments:
development:
dataSource:
dbCreate: createdrop
url: jdbc:h2:mem:devDb;MVCC=TRUE;LOCK_TIMEOUT=10000;DB_CLOSE_ON_EXIT=FALSE
test:
dataSource:
dbCreate: update
url: jdbc:h2:mem:testDb;MVCC=TRUE;LOCK_TIMEOUT=10000;DB_CLOSE_ON_EXIT=FALSE
production:
dataSource:
dbCreate: update
url: jdbc:h2:prodDb;MVCC=TRUE;LOCK_TIMEOUT=10000;DB_CLOSE_ON_EXIT=FALSE
properties:
jmxEnabled: true
initialSize: 5
...
72. Execute & Deploy
Gradle tasks (run & package)
Grails commands
– run-app environment by default 'development'
– package (executable war of jar)
– war environment by default: 'production' (to deploy in a Servlet Container)
provided "org.springframework.boot:springbootstartertomcat"
List of supported containers: https://grails.org/wiki/Deployment
@fatimacasau /{PARADIGMA TECNOLÓGICO
Microservices, Grails or Spring Boot?
$ grails [environment] runapp
$ grails package
$ grails war
73. Deploy to cloud
Since grails is build on Spring Boot and permits create
standalone applications. The way to deploy on to the cloud is
similar with Spring Boot
@fatimacasau /{PARADIGMA TECNOLÓGICO
Microservices, Grails or Spring Boot?
74. Other Important TipsOther Important Tips
@fatimacasau /{PARADIGMA TECNOLÓGICO
Microservices, Grails or Spring Boot?
75. Other important tips
HAL support
Easy integration with Spring Security
– (Grails Spring Security Core Plugin not Grails3 compatible (use Spring Security
Starter for Spring Boot)
GORM & GSP's in Spring Boot
– Your can use them outside of Grails since Grails 2.4
Grails Scaffolding
Grails autorealoading by default
Grails custom marshalling easily
@fatimacasau /{PARADIGMA TECNOLÓGICO
Microservices, Grails or Spring Boot?
77. Conclusions
@fatimacasau /{PARADIGMA TECNOLÓGICO
Microservices, Grails or Spring Boot?
With Spring Boot
Groovy language posibility
Build with gradle or maven
Convention Over Configuration
GORM (posibility)
Spock framework (posibility)
Bootstrapping
Configuration (properties, YAML)
Configuration Per Environment
Easy way to execute & deploy
Deploy to the Cloud
HAL support
With Grails 3
Groovy language
Build with gradle by default
Convention Over Configuration ++
GORM by default
Spock framework by default & Text Mixins
Bootstrapping with many config by default
Configuration (properties, YAML, Groovy DSL's)
Config. Per env. (more flixible & more features)
Easy way to execute & deploy
Deploy to the Cloud
HAL support
Plugins Library
Scaffolding
and more...
78. That's allThat's all
@fatimacasau /{PARADIGMA TECNOLÓGICO
Microservices, Grails or Spring Boot?
Source code: Spring Boot -> https://github.com/fatimacasau/spring-boot-example.git
Source code: Grails 3 -> https://github.com/fatimacasau/grails3-rest-sample.git