**
记录Springboot更改static路径后404或更改static名称后404
**
springboot默认打包文件夹是static,当有多个环境时static打包时每次都要切换,可以在对应的配置文件里面指定打包的前端文件夹
- application.properties配置
spring.resources.static-locations=classpath:/single-static/
注:如果前端index.html在文件夹下面需要加/
问题:加了single-static后其他静态资源不显示了
spring.mvc.static-path-pattern=/**:表示所有的访问都经过静态资源路径;
spring.resources.static-locations:在这里配置静态资源路径,前面说了这里的配置是覆盖默认配置,所以需要将默认的也加上否则public,META-INF等这些路径将不能被当作静态资源路径
spring.resources.static-locations=classpath:/META-INF/resources/,classpath:/resources/,classpath:/single-static/,classpath:/public/
- application.yml配置
resources:
static-locations:/single-static/
问题:配置文件添加修改前端文件夹路径配置后,发现swagger不能访问404了
通过网上查看发现如果修改springboot默认打包目录后,需要添加swagger配置
在SpringBoot中,默认配置的/**映射到/static
@Component
public class WebConfig extends WebMvcConfigurerAdapter {
@Override
public void addInterceptors(InterceptorRegistry registry) {
}
/**
* 添加静态资源处理器
* @param registry
*/
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("swagger-ui.html")
.addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**")
.addResourceLocations("classpath:/META-INF/resources/webjars/");
super.addResourceHandlers(registry);
}
}