Redirect unknown requests to index.html in springboot

后端 未结 1 1493
醉酒成梦
醉酒成梦 2020-12-21 09:21

I\'m trying to get an Angular2 app served up through a springboot web application. I\'ve found lots of examples of how to do this very simply:

https://spring.io/blog

相关标签:
1条回答
  • 2020-12-21 09:43

    As a work-a-round I've added the Angular Routes in a RequestMapping annotation and pointed them all at the index.html:

    @RequestMapping({"/login", "/logout"})
    public String index() { return "index.html"; }
    

    Edit: As a better work-a-round, you can make the controller implement ErrorController, override the getErrorPath method, then add a mapping for /error which will act as a catch-all (or mapping missing) method.

    @Controller
    public class TheOneController implements ErrorController {
    
        @RequestMapping("/error")
        public String index() {
            return "index.html";
        }
    
        @Override
        public String getErrorPath() {
            return "index.html";
        }
    }
    

    Now the index method will handle anything that can't be found and render the index.html.

    0 讨论(0)
提交回复
热议问题