Spring Boot Mustache

last modified May 20, 2022

In Spring Boot Mustache tutorial, we are going to create a simple Spring Boot web application with Mustache template engine.


Mustache is a simple web template system. It is available for many programming languages including Java. Mustache is described as a logic-less because it does not have any explicit control flow statements, such as if and else conditionals or for loops. Looping and conditional evaluation can be achieved using section tags processing lists and lambdas.

Spring Boot Mustache example

The following example is a Spring Boot web application that uses Mustache template engine.

├── main
│   ├── java
│   │   └── com
│   │       └── zetcode
│   │           ├── Application.java
│   │           └── controller
│   │               └── HelloController.java
│   └── resources
│       └── templates
│           └── hello.mustache
└── test
    ├── java
    │   └── com
    │       └── zetcode
    │           └── controller
    │               └── HelloControllerTest.java
    └── resources

This is the project structure. The template files have .mustache suffix; they are located in the src/main/resources/templates directory by default. Spring Boot automatically configures Mustache when it finds the dependency in the Maven POM file.

plugins {
    id 'org.springframework.boot' version '2.6.7'
    id 'io.spring.dependency-management' version '1.0.11.RELEASE'
    id 'java'

group = 'com.zetcode'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '17'

repositories {

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-web'
    implementation 'org.springframework.boot:spring-boot-starter-mustache'
    implementation 'org.springframework.boot:spring-boot-starter-test'

test {

This is the Gradle build file. The spring-boot-starter-mustache is starter for building Spring MVC applications with Mustache.

package com.zetcode.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

public class HelloController {

    @GetMapping(value = "/hello")
    public String hello(Model model) {

        var msg = "Hello there!";
        model.addAttribute("message", msg);

        return "hello";

We have a simple HelloController where we map the /hello URL path to the hello.mustache view. We add a short text attribute to the model for rendering in the view.

return "hello";

The returned value is the name of the view (without the suffix) to be rendered.

<!DOCTYPE html>
<html lang="en">
    <meta charset="UTF-8">

 {{ message }}


The hello.mustache is a Mustache template file that contains placeholders to be filled with data from the model. Mustache uses the {{ }} syntax.

package com.zetcode.controller;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.web.servlet.MockMvc;

import static org.hamcrest.Matchers.containsString;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;

class HelloControllerTest {

    private MockMvc mockMvc;

    public void testHome() throws Exception {

        var expected = "Hello there!";

This is a HelloControllerTest, which check for the presence of the hello message in the response.

package com.zetcode;

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

public class Application {

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

We set up the Spring Boot application. The @SpringBootApplication annotation enables auto-configuration and component scanning.

$ ./gradlew bootRun

We run the application and navigate to localhost:8080.

In this tutorial, we have created a Spring Boot web application with Mustache.

List all Spring Boot tutorials.