Create a JPA Application using Groovy and Spring Boot

You can create a Spring Boot application using Groovy either from the IDE or using the online Spring Boot application generator and selecting Groovy as the language.

You’ll now see how to develop a simple Spring Boot web application using Groovy, Spring Data JPA, and Thymeleaf. Add the Web, Thymeleaf, JPA, and H2 starters dependencies to your application.

spring init --language=groovy -dweb,thymeleaf,jpa,h2 demogroovy 

Create a JPA entity called Customer.groovy:

package com.example.demogroovy;
import javax.persistence.*@Entity @Table(name = "CUSTOMER")
class Customer {
  @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.

Create a Spring Data JPA repository for the Entity:

package com.example.demogroovy;


interface CustomerRepository extends JpaRepository<Customer, Long> {
  Customer findByName(String name);

Next, create a SpringMVC controller to show the list of customers, as shown here:

package com.example.demogroovy;
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 CustomerRepository repo;
  @GetMapping("/") String home(Model model) {
    model.addAttribute("customers", repo.findAll())

Create the Thymeleaf view src/main/resources/templates/home.html to render customers:

<html xmlns:th="">
        <META http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Customers List</title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
                             <th>Id</th>         <th>Name</th>     
                <tr th:each="Customer : ${customers}">
                             <td th:text="${}">Id</td>         <td th:text="${}">Name</td>     

When you generate the application, the main entry point class is created, as shown here:

package com.example.demogroovy;
import org.springframework.boot.SpringApplication
import org.springframework.boot.autoconfigure.SpringBootApplication @SpringBootApplication class DemoApplication {
  static void main(String[] args) {, args)

You can initialize the database with sample data using a SQL script in src/main/resources/data.sql

insert into customer(id, name, email) values (1,'john',''), (2,'frank',''), (3,'test',''); 

Finally, include in your src/main/resources/ the following property that will auto-generate the customer table at start-up:

spring.jpa.hibernate.ddl-auto = update 

Now you can run the application by executing:

 ./mvnw spring-boot:run

If you point your browser to http://localhost:8080/, you should be able to see customer details.