Mapping the root URL (/) to index.html in a Spring Boot application involves a few straightforward steps. This approach assumes you have a standard Spring Boot project structure with a src/main/resources/static directory where index.html is located. Here's how you can achieve this:
Place index.html in src/main/resources/static:
index.html is located in the src/main/resources/static directory. This is where Spring Boot serves static content from by default.src └── main └── resources └── static └── index.html
Create a Controller to Handle Root URL:
/) to serve index.html.package com.example.demo; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; @Controller public class HomeController { @GetMapping("/") public String index() { return "index"; // This will resolve to src/main/resources/static/index.html } } index() method returns "index", which is the name of the HTML file (index.html) located in the static folder. Spring Boot resolves this to serve index.html when the root URL (/) is accessed.Configure Spring Boot Application:
@ComponentScan) and enable MVC (@EnableWebMvc if needed).package com.example.demo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } } Build and Run:
mvn clean install or using an IDE).mvn spring-boot:run or run the main method from your IDE).Access index.html:
http://localhost:8080/. You should see your index.html served as the home page.index.html) from the src/main/resources/static directory by default.@RestController: Use @Controller for MVC views (index.html), while @RestController is for REST APIs returning JSON/XML.@RequestMapping("/")) that might override this mapping.By following these steps, you can map your Spring Boot application's root URL (/) to serve index.html located in the src/main/resources/static directory. This approach leverages Spring Boot's default behavior for serving static content and integrates it with a simple controller to handle the root URL mapping. Adjustments can be made based on specific project needs or additional configurations required.
Spring Boot map root URL ("/") to index.html
index.html.import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; @Controller public class HomeController { @GetMapping("/") public String index() { return "index"; } } Java Spring Boot set default page to index.html
index.html as the default landing page for the root URL ("/").import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; @SpringBootApplication @Controller public class MyApp { public static void main(String[] args) { SpringApplication.run(MyApp.class, args); } @GetMapping("/") public String index() { return "index"; } } Spring Boot serve index.html as home page
index.html when accessing the root URL ("/").import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; @Controller public class HomeController { @RequestMapping(value = "/", method = RequestMethod.GET) public String index() { return "index"; } } Java Spring Boot redirect root to index.html
index.html in a Spring Boot application.import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; @Controller public class HomeController { @GetMapping("/") public String index() { return "redirect:/index.html"; } } Spring Boot default landing page index.html
index.html as the default landing page for the root URL ("/") in a Spring Boot project.import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; @Controller public class HomeController { @GetMapping("/") public String index() { return "index"; } } Java Spring Boot serve index.html from resources
index.html located in the resources/static folder.import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; @Controller public class HomeController { @GetMapping("/") public String index() { return "index"; } } Spring Boot serve Angular index.html from static folder
index.html file from the static folder in a Spring Boot application.import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; @Controller public class HomeController { @GetMapping("/") public String index() { return "index"; } } Java Spring Boot set root URL to serve index.html
index.html.import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; @Controller public class HomeController { @GetMapping("/") public String index() { return "index"; } } Spring Boot load index.html as default page
index.html as the default landing page for the root URL ("/") in a Spring Boot project.import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; @Controller public class HomeController { @GetMapping("/") public String index() { return "index"; } } Java Spring Boot map root URL to index.html Thymeleaf
index.html using Thymeleaf in a Spring Boot application.import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; @Controller public class HomeController { @GetMapping("/") public String index() { return "index"; } } bower sql-loader facelets django-forms scikit-learn gps move compiler-construction ios7 angular2-router