Spring Boot Scala Thymeleaf Example Tutorial

In this tutorial, you will learn how to develop a simple web application using Spring Boot and Scala.
Scala is one of the most popular JVM-based programming languages. It mixes functional programming and object-oriented programming idioms into a single language.

1. Creating Spring Boot Project

Spring Initializer (http://start.spring.io) doesn’t provide support for Scala yet, but you can create Spring Boot applications using the Scala programming language by configuring appropriate plugins based on the build tool you are using.
If you want to use Maven as a build tool, you can use scala-maven-plugin to compile your Scala code. 

2. Add Maven Dependencies

Add the Web, Spring Data JPA, Thymeleaf, and H2 starter dependencies.
<?xml version="1.0" encoding="UTF-8"?>
 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <description>Demo project for Spring Boot</description>
        <!-- lookup parent from repository -->

3. User.scala

Create a JPA entity called User.scala and add the following content to it:
import javax.persistence._

import scala.beans.BeanProperty

@Table(name = "users")
class User {

    @GeneratedValue(strategy = GenerationType.AUTO)
    var id: Long = _

    var name: String = _

    var email: String = _

You are using the @BeanProperty to generate setters and getters for fields based on the JavaBean naming conventions.

4. UserRepository.scala

Now you’ll see how to create the Spring Data JPA repository for the User entity. As Scala doesn’t have interfaces, you are going to create UserRepository as a trait:
import org.springframework.data.jpa.repository.JpaRepository
import org.springframework.data.repository.query.Param

trait UserRepository extends JpaRepository[User, java.lang.Long] {
  def findByEmail(@Param("email") name: String): List[User]

5. HomeController.scala

Let's create a Spring MVC 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. {

class HomeController {
    var repo: UserRepository = _

    def home(model: Model): String = {
        model.addAttribute("users", repo.findAll())

6. data.sql

Here is a data.sql script to populate sample users:
delete from users;

insert into users(id, name, email) values

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

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

8. Application 

Next, you are going to create the application main entry point class. As Scala doesn’t support static methods, you have to create a companion object and write the main() method:
import org.springframework.boot.SpringApplication
import org.springframework.boot.autoconfigure.SpringBootApplication

class Application {


object Application {
    def main(args: Array[String]) : Unit = {
        SpringApplication.run(classOf[Application], args:_*)
Now you can run Application.main(), which starts your Spring Boot application and accesses it at http://localhost:8080/. You can also use below command to run this spring boot app:
springboot-scala-demo> mvn spring-boot:run
This shows a list of users.


  1. I wanted to thank you for this great Information and knowledge, I definitely loved every little bit of it. I have you bookmarked your web site to check out the latest stuff you post. Your blog is eye-catching. I get pleasure from it. Thanks for sharing this beautiful piece of writing with me, get more information at Website Design And Development Company for more Metricoid Technology Solutions related information and knowledge.


Post a comment