Java Spring Boot How to map my app root “/” to index.html?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Java Spring Boot is a powerful, rapid-development framework used for creating standalone, production-grade Spring-based applications. It simplifies the development process by auto-configuring many components required in a typical Spring application. One of the common tasks in web application development is to map the root context ("/") of your application to a index.html file. This can be useful for setting a default page or UI landing page for your application.
In this article, we'll explore how to perform this mapping in a Spring Boot application, alongside other related configuration tips.
Setting Up the Spring Boot Application
First, ensure you have a basic Spring Boot setup. If you are creating a new project, you can use Spring Initializr or configure it manually. Here's a quick setup guide using Maven:
If you prefer Gradle, your build.gradle will look like this:
Mapping Root URL to index.html
The simplest way to map the root URL to the index.html file is to place your index.html inside the src/main/resources/static directory. Spring Boot automatically maps static resources located in src/main/resources/static, src/main/resources/public, src/main/resources/resources, and src/main/resources/META-INF/resources as they are served directly.
With the index.html file under src/main/resources/static, any request to the root URL ("/") will automatically serve this file.
Custom Controller for Advanced Routing
For more complex routing requirements (e.g., conditionally rendering different pages), you may need to define a custom controller.
Here is an example of how you might define a controller to handle the root URL:
In this scenario, index.html should reside in src/main/resources/templates if you are using a templating engine like Thymeleaf, rather than serving static content.
Configuring Welcome Page
You can also set a welcome page using Spring Boot's application properties. Add the following to src/main/resources/application.properties:
This configuration allows Spring Boot to automatically serve index.html from the specified classpath location at the root URL.
Creating an Example index.html
Here’s a simple example of what your index.html might look like:
Summary Table
| Topic | Description |
| Static Resource Mapping | Place index.html under static, public, resources, or META-INF/resources for automatic mapping to "/" URL. |
| Custom Controller | Use @GetMapping("/") and return view name for advanced routing.
File should be in templates directory. |
| Configuration Properties | spring.web.resources.add-mappings=true to ensure static mappings are active. |
| File Location | Static content in src/main/resources/static.
Templates in src/main/resources/templates. |
Additional Details and Tips
- Disable Spring Boot Defaults: If you want to disable the default behavior of serving static resources, you can exclude the auto-configuration for static content by adding
@SpringBootApplication(exclude = {WebMvcAutoConfiguration.class}). - Use of Other Templating Engines: If you use other templates like Freemarker or Mustache, ensure appropriate view resolvers are configured.
- Custom Error Page: You can create a default error page by simply adding an
error.htmlfile in the static resource directory, which will be served when an error occurs.
In conclusion, Spring Boot makes routing straightforward both through auto-mapping static content and using custom controllers for dynamic routing needs. By leveraging Spring Boot's conventions, you can efficiently manage resources and customize behavior according to your application's requirements.
Related reading
- Java Spring Boot How to map my app root “/” to index.html?
- Java Spring Boot Test How to exclude java configuration class from test context
- Java Spring REST API Handling Many Optional `Parameters`
- Java Spring Security - User.withDefaultPasswordEncoder is deprecated?
- Javadoc package.html or package-info.java
- JavaScript - get the first day of the week from current date
- Java Stanford NLP Part of Speech labels?
- Java Static vs inner class

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.