Spring catch all route for index.html

后端 未结 6 1933

I\'m developing a spring backend for a react-based single page application where I\'m using react-router for client-side routing.

Beside the index.html page the back

6条回答
  •  生来不讨喜
    2020-11-29 00:13

    Avoid @EnableWebMvc

    By default Spring-Boot serves static content in src/main/resources:

    • /META-INF/resources/
    • /resources/
    • /static/
    • /public/

    Take a look at this and this;

    Or keep @EnableWebMvc and override addViewControllers

    Did you specify @EnableWebMvc ? Take a look a this: Java Spring Boot: How to map my app root (“/”) to index.html?

    Either you remove @EnableWebMvc, or you can re-define addViewControllers:

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/").setViewName("forward:/index.html");
    }
    

    Or define a Controller to catch /

    You may take a look a this spring-boot-reactjs sample project on github:

    It does what you want using a Controller:

    @Controller
    public class HomeController {
    
        @RequestMapping(value = "/")
        public String index() {
            return "index";
        }
    
    }
    

    Its index.html is under src/main/resources/templates

提交回复
热议问题