o7planning

Spring Boot and Groovy Tutorial with Examples

  1. What is Groovy ?
  2. Create a Spring Boot project
  3. Controller, Groovy Template
  4. Run the application

1. What is Groovy ?

Apache Groovy is an object-oriented programming language for the Java platform. It is a dynamic language with features similar to those of Python, Ruby, Perl, and Smalltalk. It can be used as a scripting language for the Java Platform, is dynamically compiled to Java virtual machine (JVM) bytecode, and interoperates with other Java code and libraries. Groovy uses a Java-like curly-bracket { } syntax. Most Java code is also syntactically valid Groovy, although semantics may be different.
In this lesson, I am not going to focus on introducing Groovy language as well as not using this language. But the Groovy provides a template in order to create HTML documents, and this is the subject that we are going to discuss in the lesson.
The objective of this lesson is to create a Java Web application using Spring Boot and Groovy Template for View layer. Of course, for the View layer, you can select other technologies such as JSP, Thymeleaf ...

2. Create a Spring Boot project

On the Eclipse, create a Spring Boot project:
To use the Groovy, you need to add spring-boot-starter-groovy-templates dependencies to your project.
<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-groovy-templates</artifactId>
</dependency>
The full content of the pom.xml file:
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
         http://maven.apache.org/xsd/maven-4.0.0.xsd">
         
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.o7planning</groupId>
    <artifactId>SpringBootGroovy</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>SpringBootGroovy</name>
    <description>Spring Boot + Groovy</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.0.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-groovy-templates</artifactId>
        </dependency>
        
        
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>
SpringBootGroovyApplication.java
package org.o7planning.sbgroovy;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SpringBootGroovyApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringBootGroovyApplication.class, args);
    }
    
}

3. Controller, Groovy Template

Create an index.tpl file in the templates folder.
index.tpl
yieldUnescaped '<!DOCTYPE html>'
html(lang:'en') {
    head {
        meta('http-equiv':'"Content-Type" content="text/html; charset=utf-8"')
        title('Person List')
    }
    body {
        h2 ('A Groovy View with Spring Boot')
        
        h3 ("Message: $message")
       
        table (border: "1")  {
            tr {
               th("First Name")
               th("Last Name")
            }
            persons.each { person ->
                tr {
                   td("$person.firstName")
                   td("$person.lastName")
                }
            }
        }
    }
}
MainController.java
package org.o7planning.sbgroovy.controller;

import java.util.ArrayList;
import java.util.List;

import org.o7planning.sbgroovy.model.Person;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class MainController {

    private static List<Person> persons = new ArrayList<Person>();

    static {
        persons.add(new Person("Bill", "Gates"));
        persons.add(new Person("Steve", "Jobs"));
    }

    @RequestMapping(value = "/")
    public String handleRequest(Model model) {
        
        String message = "Person List:";
        
        model.addAttribute("message", message);
        model.addAttribute("persons", persons);

        return "index";
    }

}
Person.java
package org.o7planning.sbgroovy.model;

public class Person {

    private String firstName;
    private String lastName;

    public Person() {

    }

    public Person(String firstName, String lastName) {
        this.firstName = firstName;
        this.lastName = lastName;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

}
The following image explains the relationship between Controller and Groovy View:
  • TODO Image.

4. Run the application

Right click on the project, select:
  • Run As/Spring Boot App
At this moment, your application is run. On the browser, access the following path:

Spring Boot Tutorials

Show More