spring boot 配置静态html,js,css文件,访问服务器主页为html

首先转一个spring boot的application配置文件的属性详解:https://blog.****.net/lpfsuperman/article/details/78287265本文大部分属性详情都是从此文中得知,感谢作者的翻译。

首先我们的项目是前后端分离的,前端所使用的vue,先把他npm run build打包,然后把打包好的html,js,css文件放入到项目的resources文件下,如图我在resources下新建了一个static文件夹。

spring boot 配置静态html,js,css文件,访问服务器主页为html

然后我们就可以在application.yaml中配置spring boot引入静态文件的相关配置了,代码如下

server: 
    port: 8080
    contextPath: /
    address: hello.com
spring:
    mvc:
        static-path-pattern: /**
    view:
        prefix: /static/
        suffix: .html
resources:
    static-locations: classpath:/static/
#默认值为 classpath:/METAINF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/
优先级也是这么排的,优先classpath:/METAINF/resources/

然后输入http://hello.com:8080就可以直接访问你放入的html页面了

*留有一个问题,serve.servelet.context-path: /zf/**   这样写就不对,输入http://hello.com:8080/zf  并不能访问主页,但是可以加载js和css