两种方式配置url映射到非默认路径(/static , /public, /resources ,/META-INF/resources)
1、方法一,修改application.properties配置文件
例如,讲url为static的请求映射到static路径下
spring.mvc.static-path-pattern=/static/**
spring.resources.static-locations=classpath:/static/
2、方法二,继承WebMvcConfigurerAdapter,重载addResourceHandlers。
@Component
public class MyResHandler extends WebMvcConfigurerAdapter {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
// Swagger2 配置
registry.addResourceHandler("doc.html").addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
super.addResourceHandlers(registry);
}
}
友情提醒:
1、addResourceLocations中记得使用classpath,且以/结尾,表示路径
2、springboot工程中resources文件夹不会发布在工程目录下,所以,”classpath:/static/” 不要写成”classpath:/resources/static/”
cover:https://blog.csdn.net/biren_wang/article/details/78947558