springboot通过接口访问html,springboot不通过controller直接访问静态html
默認配置下,通過springboot搭建的web服務的資源中,可直接訪問只有"classpath:/META-INF/resources/", "classpath:/resources/", "classpath:/static/", "classpath:/public/"這幾個目錄下的,源碼在類ResourceProperties中,如下:
public class ResourceProperties {
private static final String[] CLASSPATH_RESOURCE_LOCATIONS = new String[]{"classpath:/META-INF/resources/", "classpath:/resources/", "classpath:/static/", "classpath:/public/"};
private String[] staticLocations;
private boolean addMappings;
private final ResourceProperties.Chain chain;
private final ResourceProperties.Cache cache;
public ResourceProperties() {
this.staticLocations = CLASSPATH_RESOURCE_LOCATIONS;
this.addMappings = true;
this.chain = new ResourceProperties.Chain();
this.cache = new ResourceProperties.Cache();
}
...
}
也可以在application.properties/.yml中配置spring.resources.static-locations來覆蓋默認配置,例如(application.yml):
spring:
resources:
static-locations: classpath:/static/, classpath:/templates/
以下是配置靜態資源目錄的源碼:
public void setStaticLocations(String[] staticLocations) {
this.staticLocations = this.appendSlashIfNecessary(staticLocations);
}
private String[] appendSlashIfNecessary(String[] staticLocations) {
String[] normalized = new String[staticLocations.length];
for(int i = 0; i < staticLocations.length; ++i) {
String location = staticLocations[i];
normalized[i] = location.endsWith("/") ? location : location + "/";
}
return normalized;
}
總結
以上是生活随笔為你收集整理的springboot通过接口访问html,springboot不通过controller直接访问静态html的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: flarum论坛如何html,简约个人小
- 下一篇: 计算机应用基础考证理论知识,《计算机应用