1

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.

MJBZA
  • 4,796
  • 8
  • 48
  • 97
  • [https://stackoverflow.com/questions/29085295/spring-mvc-restcontroller-and-redirect](https://stackoverflow.com/questions/29085295/spring-mvc-restcontroller-and-redirect) provides some options for `@RestController` re-directs (the title is misleading) – Allen D. Ball Jul 31 '20 at 16:31
  • You can use solution at https://stackoverflow.com/questions/7976173/url-rewriting-in-java-and-spring – Vy Do Aug 01 '20 at 09:04
  • @DoNhuVy: I tested it, unfortunately it does not inject `index.html` in the url. – MJBZA Aug 03 '20 at 05:45
  • I think Spring Boot, Spring MVC can return URL like this `http://localhost:8080/foo/bar` , not `http://localhost:8080/foo/bar.html` , therefore it is not necessary. – Vy Do Aug 03 '20 at 07:05

1 Answers1

1

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";
    }
}
MJBZA
  • 4,796
  • 8
  • 48
  • 97