Spring Boot Groovy Thymeleaf Example Tutorial

In this tutorial, you will learn how to develop a simple Spring Boot web application using Groovy, Spring Data JPA, and Thymeleaf.

1. Creating Spring Boot Project

You can create a Spring Boot application using Groovy either from the IDE or using the online Spring Boot application generator http://start.spring.io and selecting Groovy as the language.

2. Add Maven Dependencies

Add the Web, Thymeleaf, JPA, and H2 starters dependencies to your application:
<?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>net.javaguides</groupId>
    <artifactId>springboot-groovy-demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>
    <name>springboot-groovy-demo</name>
    <description>Demo project for Spring Boot</description>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.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-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-rest</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.codehaus.groovy</groupId>
            <artifactId>groovy</artifactId>
        </dependency>
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <scope>runtime</scope>
        </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>

3. User.groovy

Let's create a JPA entity called User.groovy and add the following content to it:
import javax.persistence.*

@Entity
@Table(name = "users")
class User {
    @Id @GeneratedValue(strategy = GenerationType.AUTO)
    Long id
    String name
    String email
}
As you are using Groovy, you don’t need to create setters and getters for your entity properties.

4. UserRepository.groovy

Create a Spring Data JPA repository for the User entity:
import org.springframework.data.jpa.repository.JpaRepository

interface UserRepository extends JpaRepository<User, Long> {
    User findByEmail(String email);
}

5. HomeController.groovy

Let's create a SpringMVC controller to show the list of users:
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Controller
import org.springframework.ui.Model
import org.springframework.web.bind.annotation.GetMapping

@Controller
class HomeController {

    @Autowired
    UserRepository repo;

    @GetMapping("/")
    String home(Model model) {
        model.addAttribute("users", repo.findAll())
        "home"
    }
}

6. src/main/resources/templates/home.html

Create the Thymeleaf view home.html to render users:
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>Users List</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<table>
    <thead>
    <tr>
        <th>Id</th>
        <th>Name</th>
    </tr>
    </thead>
    <tbody>
    <tr th:each="user : ${users}">
        <td th:text="${user.id}">Id</td>
        <td th:text="${user.name}">Name</td>
    </tr>
    </tbody>
</table>
</body>
</html>

7. src/main/resources/data.sql

Initialize the database with sample data using a SQL script:
insert into users(id, name, email) values
(1,'admin','admin@gmail.com'),
(2,'john','john@gmail.com'),
(3,'test','test@gmail.com');

8. SpringbootGroovyDemoApplication.groovy

When you generate the application, the main entry point class is created:
import org.springframework.boot.SpringApplication
import org.springframework.boot.autoconfigure.SpringBootApplication

@SpringBootApplication
class SpringbootGroovyDemoApplication {

 static void main(String[] args) {
  SpringApplication.run SpringbootGroovyDemoApplication, args
 }
}

9. Run Spring Boot App

Now you can run the application by executing the SpringbootGroovyDemoApplication.main() method or using the following command:
mvn spring-boot:run or gradle bootRun 
If you point your browser to http://localhost:8080/, you should be able to see user details


Comments