Corporate Training
Request Demo
Click me
Menu
Let's Talk
Request Demo

Tutorials

Groovy for Web Development

19. Groovy for Web Development (Grails, Ratpack)

Groovy is an excellent choice for web development due to its concise syntax, dynamic features, and seamless integration with Java libraries. In this tutorial, we'll explore two popular Groovy-based web frameworks: Grails and Ratpack.

Grails: A Full-Stack Web Framework

Grails is a high-productivity, full-stack web framework that leverages the Groovy language's power. It follows the Convention over Configuration (CoC) and Don't Repeat Yourself (DRY) principles, making it easy to build web applications quickly.

Setting Up a Grails Project:

Before we dive into Grails, make sure you have Grails installed. You can install it using the SDKMAN! tool or by downloading it from the Grails website.

1. Create a new Grails project:
grails create-app mywebapp
cd mywebapp
 
2. Generate a domain class:
grails create-domain-class com.example.Book
 
3. Edit 'grails-app/domain/com/example/Book.groovy':
package com.example

class Book {
    String title
    String author
    String genre
}
 
4. Generate a controller and views:
package com.example

import spock.lang.Specification

class MainSpec extends Specification {

    def "Adding two numbers"() {
        given:
        def main = new Main()

        when:
        def result = main.add(2, 3)

        then:
        result == 5
    }
}
 
5. Start the Grails application:
grails run-app
 

Your Grails application is now running. Access it at 'http://localhost:8080/mywebapp/book/list'.

Ratpack: A Lightweight, Non-blocking Framework

Ratpack is a lightweight and asynchronous web framework for Groovy. It's known for its non-blocking and reactive programming model, making it suitable for high-performance applications.

Setting Up a Ratpack Project:

Before you start with Ratpack, make sure you have Java and Gradle installed. Ratpack projects are typically managed using Gradle.

1. Create a new Gradle Ratpack project:
mkdir myratpackapp
cd myratpackapp
gradle init --type java-library
 
2. Edit 'build.gradle'to include the Ratpack dependency:
dependencies {
    implementation 'org.ratpackframework:ratpack-core:2.7.0'
    testImplementation 'junit:junit:4.13.2'
}
 
3. Create a Ratpack handler:
// src/main/groovy/com/example/MyHandler.groovy

package com.example

import ratpack.func.Action
import ratpack.handling.Context

class MyHandler implements Action {
    @Override
    void execute(Context context) {
        context.render("Hello, Ratpack!")
    }
}
 
4. Edit 'src/main/groovy/com/example/MyRatpackApp.groovy':
package com.example

import ratpack.groovy.GroovyRatpackMain

class MyRatpackApp {
    static void main(String[] args) {
        GroovyRatpackMain.run {
            handlers {
                get(new MyHandler())
            }
        }
    }
}
 
5. Run the Ratpack application:
./gradlew run
 

Your Ratpack application is now running. Access it at 'http://localhost:5050' .

Conclusion:

Groovy is a versatile language for web development, and it offers options for both full-stack and lightweight, non-blocking web frameworks like Grails and Ratpack. Depending on your project requirements, you can choose the framework that best fits your needs. Whether you prefer the convention-based simplicity of Grails or the performance-focused nature of Ratpack, Groovy's expressiveness and integration capabilities make it a strong choice for web development.