Spring Boot Hello World – Freemarker tutorial

Tutorial describing how to create the simple Hello World application using Spring Boot and Freemarker template engine.

Technologies used:

  1. Spring Boot 2.1.7.RELEASE
  2. JDK 1.8
  3. Maven 3
  4. Freemarker

Project setup

Project dependencies managed by the Maven’s pom.xml configuration file:

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">








The explanation of the above dependencies:

  • spring-boot-starter-web – packages all the necessary dependencies together with the auto configuration for running simple web application
  • spring-boot-starter-tomcat – provides the embedded tomcat application server
  • spring-boot-starter-freemarker – provides auto configuration for Freemarker templates
  • spring-boot-maven-plugin – plugin prividing commands to work with a Spring Boot application (we will be usine the spring-boot:run command)

Application Initializer

The standard spring boot application initializer, which our application need to provide:

public class Application extends SpringBootServletInitializer {

protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(Application.class);

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


The standard controller containing rest services:

public class HelloWorldController {

private static final String message = "Hello World";

@GetMapping(value = "/")
public String hello(Map<String, Object> model) {
model.put("message", message);
return "hello";

The controller has only one endpoint available at the root address (/ – in our example it is resolved to the localhost:8080) . The method passes the message parameter to the hello template.


<!DOCTYPE html>
<html lang="en">
    <meta charset="UTF-8">
    <h2 class="hello-title">${message}</h2>

The view contains a simple html page, that shows passed message parameter.


The configuration in application.properties file contains information how to resolve Freemarker files. In our case, the templates (with suffix .ftl) are located under resources the resources/ftl directory:

spring.freemarker.template-loader-path: classpath:/ftl
spring.freemarker.suffix: .ftl


The project can be started by the running the following command:

mvn clean spring-boot:run

The command deploys the apllication on the embedded tomcat server.

By visiting the localhost:8080 url, we should see the page containg the Hello World message.

The example code you can download from here

Leave a Reply

Your email address will not be published. Required fields are marked *