How to serve static html content page in spring-boot

前端 未结 4 1512
孤独总比滥情好
孤独总比滥情好 2020-12-28 15:17

I\'m starting an embedded tomcat via spring-boot and want to serve a static index.html page as part of a running application.

But the follo

相关标签:
4条回答
  • 2020-12-28 15:18

    My fault: I had an additional class with @EnableWebMvc annotation. This somehow messed up the spring-boot autoconfiguration. I removed it and now it works returning index.html.

    0 讨论(0)
  • 2020-12-28 15:25

    For me this worked, i am sure there is a better way ( like without .html ).

    @RequestMapping("/")
    public String index() {
        return "index.html";
    }
    
    0 讨论(0)
  • 2020-12-28 15:37

    You can use ModelAndView in order to serve static HTML content in spring boot.

    @RequestMapping("/")
    public ModelAndView home()
    {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("index");
        return modelAndView;
    }
    

    application.properties:-

    spring.mvc.view.suffix = .html

    HTML File : - src/main/resources/static/index.html

    0 讨论(0)
  • 2020-12-28 15:38

    Thats because of @RestController annotation, just removing this annotation works for me.

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