This tutorial walks you through the steps of creating a Hello World web app example with Spring Boot and FreeMarker

What you'll build

What you'll need

  • JDK 8+ or OpenJDK 8+
  • Maven 3+

The tech stack

  • Spring Boot is a popular Java based framework used to build production-grade web applications and services. You can find more Spring Boot tutorials here

  • FreeMarker is a Java based server-side template engine

Init project structure

You can create and init a new Spring Boot project by using Spring CLI or Spring Initializr. Learn more about using these tools at here

The final project structure as below

├── src
│   └── main
│       ├── java
│       │   └── com
│       │       └── hellokoding
│       │           └── springboot
│       │               └── view
│       │                   ├── Application.java
│       │                   └── HelloController.java
│       └── resources
│           ├── static
│           │   ├── css
│           │   │   └── main.css
│           │   └── js
│           │       └── main.js
│           ├── templates
│           │   └── hello.ftl
│           └── application.properties
└── pom.xml

Project dependencies

Add spring-boot-starter-web and spring-boot-starter-freemarker into your project as a dependency on pom.xml or build.gradle file. The library versions can be omitted as it is resolved by the parent pom provided by Spring Boot

<dependencies>  
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-freemarker</artifactId>
    </dependency>
</dependencies>  

You can find the full pom.xml file as below


Define Controller


@Controller indicates the annotated class is a web controller

@GetMapping maps HTTP GET request for "/" (home page) and "/hello" to the hello method

@RequestParam binds method parameter name to request query string parameter (value="name",...)

Model is an object sharing data between handler and view template

The view template name is defined by the return statement of the handler and the spring.freemarker.suffix config property which defined in the below application.properties file. So in this hello handler method, the return view is hello.ftl

View Template


Static files



Application Configurations



Run and Test

You can run the application by typing the following command on terminal console at the project root directory

$ mvn clean spring-boot:run

You would see this text in the console

o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''  

Access to http://localhost:8080 on your web browser, the following response is expected

Hello, World!

Source code

https://github.com/hellokoding/hellokoding-courses/tree/master/springboot-examples/springboot-freemarker-helloworld