Spring Boot and Groovy Tutorial

View more categories:

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.
The full content of the  pom.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"


    <description>Spring Boot + Groovy</description>

        <relativePath/> <!-- lookup parent from repository -->





package org.o7planning.sbgroovy;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.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.
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 {
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;

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";

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::

View more categories: