Configure Spring Boot for SPA frontend

前端 未结 3 743
谎友^
谎友^ 2020-12-10 19:21

I have application where whole frontend part is laying in resource. I would like to separate things apart. And have separate server for UI, provided by gulp, for example.

3条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-10 19:58

    EpicPandaForce has a great answer, and I wanted to expand on it. The following endpoint will allow matching on nested routes as well. If you wanted to have an admin section, you can configure it to return a different index.html.

    @Controller
    class PageController {
    
        @GetMapping("/**/{path:[^\\.]*}")
        fun forward(request: HttpServletRequest): String? {
            if(request.requestURI.startsWith("/admin")) {
                return "forward:/admin/index.html"
            }
            return "forward:/index.html"
        }
    }
    

    This RequestMapping (or @GetMapping) works by excluding any request that contains a period (i.e. "index.html").

提交回复
热议问题