How to add rewrite rule to spring boot 2.3.1

Tags: , , ,



I have a Spring Boot application based on this example.

Now the question is how can I add rewrite rules to my application that add /index.html when user visit the root URL.

I mean when user visit http://localhost:8080/my-app or http://localhost:8080/my-app/ then I redirect him or her to http://localhost:8080/my-app/index.html.

I found something here, but unfortunately does not work for me, also it seems org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory does not exist in Spring Boot 2.3.1 anymore.

Answer

I only need to add a new controller, despite this application does not use MVC, this controller will redirect the / requests to /index.html.

package me.cimply.ask.odata.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

@Controller
public class AppController {
    
    @GetMapping("/")
    public String index() {     
        return "redirect:index.html";
    }
}


Source: stackoverflow