Spring boot cannot find index.html under webapp folder

后端 未结 2 1385
清歌不尽
清歌不尽 2020-12-06 01:58

I read the following documentation from spring.io and it said By default Spring Boot will serve static content from a directory called /static (or /public or /resource

相关标签:
2条回答
  • 2020-12-06 02:33
    @RestController
    public class IndexController {
    
        @RequestMapping("/")
        public String index(){
            System.out.println("Looking in the index controller.........");
            return "index";
        }
    
    }
    

    Problem is here, you are using @RestController, so in this case, if you write "return 'index';" spring boot covers it as just string answer. You need use @Controller annotation instead.

    0 讨论(0)
  • 2020-12-06 02:38

    Spring Boot docs also says:

    Do not use the src/main/webapp directory if your application will be packaged as a jar. Although this directory is a common standard, it will only work with war packaging and it will be silently ignored by most build tools if you generate a jar.

    Spring Boot is very opinionated and works best when you do not try to resist defaults. I don't see any reason having your files placed in /src/main/webapp. Just use /src/main/resources/static for your front-end assets. That is most common place.

    It will serve these static files from root URI automatically, without need to create any root level Controller. In fact your IndexController would prevent static front-end files to be served from root URI. There is no need to create Controller for static files at all.

    Also view resolver is not needed for your app. Your app is just REST API consumed by single page angular application. So your HTML templating is on client. View resolvers are needed if you are doing server side HTML templating (e.g. with Thymeleaf or JSP). So remove that piece also.

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