Returning an HTML Page From a RESTful Controller in Spring Boot
Last Updated :
20 May, 2024
In Spring Boot applications, RESTful controllers are commonly used to handle HTTP requests and return appropriate responses. These responses are typically in JSON or XML format. However, sometimes there is a need to return HTML pages, which the server can dynamically generate.
In Spring Boot, we can configure a controller that handles requests and renders HTML pages using the Thymeleaf template. Thymeleaf enables us to create dynamic HTML pages by directly embedding server-side variables and expressions into HTML code. The controller method will process the incoming HTTP request, process any necessary data, and pass it to the Thymeleaf template. The Thymeleaf template will render the HTML page with dynamic text before returning it to the client.
Key Terminologies:
- RESTful Controller: These are Java classes annotated with
@RestController
or @Controller
. They handle HTTP requests and return responses, typically in JSON or XML, to build RESTful web applications. - Thymeleaf: This is a Java template engine for web and standalone environments. It allows us to create dynamic HTML pages by directly embedding server-side variables and expressions into HTML code.
- Template Engine: A software module that processes template files to generate output documents based on data and template logic. Thymeleaf is an example used in Spring Boot applications.
- Model: Represents data transferred between the controller and the view. In Spring controllers, it's often represented as a
Model
object, allowing attributes to be passed to the view for rendering. - View: Responsible for presenting data to the user. In Spring Boot applications with Thymeleaf, views are typically HTML templates with dynamic text and Thymeleaf annotations.
- Controller: Processes user requests, handles input data, and returns responses. Spring Boot controllers are Java classes annotated with
@Controller
or @RestController
.
Step-by-step Implementation to Return an HTML Page From a RESTful Controller in Spring Boot
Below are the implementation steps to return an HTML page from a RESTful controller in Spring Boot application.
Step 1: Create the Spring Boot Project
We create a new Spring Boot project using Spring Initializr, including the dependencies listed below into the project.
Dependencies:
- Spring Web
- Thymeleaf
- Lombok
- Spring Dev tools
Once complete the creation of the project then the file structure looks like the below image.

Step 2: Create the Controller class
Go to src > org.example.springhtmldemo > Greeting Controller and put the code below:
Java
package org.example.springhtmldemo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
@Controller
public class GreetingController {
@GetMapping("/greeting")
public String greeting(Model model) {
model.addAttribute("message", "Welcome to our website!");
return "index"; // Thymeleaf template name (greeting.html)
}
}
Step 3: Default main class
Go to src > org.example.springhtmldemo > SpringHtmlDemoApplication and put the code below:
Java
package org.example.springhtmldemo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringHtmlDemoApplication {
public static void main(String[] args) {
SpringApplication.run(SpringHtmlDemoApplication.class, args);
}
}
Step 4: Create the HTML page
Go to src > java > resources > templates > index.html and put the HTML code below:
HTML
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Greeting Page</title>
<style>
body {
font-family: Arial, sans-serif;
text-align: center;
}
.container {
margin-top: 100px;
}
.greeting {
font-size: 24px;
margin-bottom: 20px;
}
.button {
padding: 10px 20px;
background-color: #007bff;
color: #fff;
border: none;
border-radius: 4px;
cursor: pointer;
font-size: 16px;
transition: background-color 0.3s;
}
.button:hover {
background-color: #0056b3;
}
</style>
</head>
<body>
<div class="container">
<h1 class="greeting" th:text="${message}"></h1>
<button class="button" onclick="sayHello()">Say Hello</button>
</div>
<script>
function sayHello() {
alert('Hello, there!');
}
</script>
</body>
</html>
Step 5: Run the Application
Once we complete the project, we can run the application. After successfully running the application, it will look like the image below.

Output HTML page:

If we follow the above steps, we can successfully create a new HTML page with a better UI and configure the controller to return it in the Spring Boot application.
Similar Reads
Add Prefix to All Spring Boot Controllers The Spring Boot framework provides a lot of features for solving real-time problems in the software industry. Here we will discuss how to add a prefix to all APIs or Controller in the Spring Boot Application. The prefix means The APIs have a common part in the URL or API endpoints. We can achieve th
6 min read
Returning Errors Using ProblemDetail in Spring Boot In a Spring Boot application, error handling is an important aspect of providing a robust and user-friendly API. Traditionally, Spring Boot has provided various methods like @ExceptionHandler, ResponseEntityExceptionHandler, and @ControllerAdvice for handling exceptions. With Spring Framework 6 and
7 min read
Read file from Resources Folder in Spring Boot Spring Boot is a framework for handling web applications, and it is easy to create stand-alone applications. The Spring Boot framework is an open-source Java-based framework mostly used for creating microservices. In this article, we will discuss how to read file from the resources folder in Spring
4 min read
Returning an Image or a File with Spring In web development, it is common for servers to send various types of content to clients, including static and dynamic data, images, and files. The Spring Framework provides powerful tools to handle these requirements efficiently, allowing developers to return images and files directly from endpoint
3 min read
Spring Boot: Cannot access REST Controller on localhost (404) The Spring Boot framework is one of the most famous in web application development, It can provide a lot of features to make development easy like dependency management, Spring Security, Spring Autoconfiguration, and other features provided by the Spring Framework. In this article, we will learn how
4 min read
Modify Request Body Before Reaching Controller in Spring Boot In Spring Boot applications, efficiently handling HTTP requests is crucial, especially when it comes to modifying or validating the data before it reaches the controller. This might involve tasks such as formatting, logging, adding metadata, or sanitizing input. This article explores how to modify t
6 min read