springboot 2.X 在访问静态资源的的时候出现404的问题
通过idea快速搭建一个springboot项目:
springboot版本2.1.6
在网上看的资料,springboot静态资源访问如下:
"classpath:/META‐INF/resources/",
"classpath:/resources/",
"classpath:/static/",
"classpath:/public/"
"/":当前项目的根路径
之后在测试访问静态资源的时候没有问题,通过代码配置的方式访问登录页面资源的时候出现了404。
在网上找了很多关于静态资源访问404的问题,貌似不是在application.yml(或properties)中配置访问路径,就是自己定义config类。
后来翻看源码发现,由于在代码配置的时候。继承了WebMvcConfigurationSupport这个类,导致整个资源访问失效,具体原因如下:
第一步:
确认springboot静态资源访问的位置,由于springboot自动加载配置,所以找到WebMvcAutoConfiguration这个配置类,里面有很多方法,
只需找到资源映射的方法:
public void addResourceHandlers(ResourceHandlerRegistry registry) {
if (!this.resourceProperties.isAddMappings()) {
logger.debug("Default resource handling disabled");
} else {
Duration cachePeriod = this.resourceProperties.getCache().getPeriod();
CacheControl cacheControl = this.resourceProperties.getCache().getCachecontrol().toHttpCacheControl();
<!-- /webjars/** 访问的,资源映射规则-->
if (!registry.hasMappingForPattern("/webjars/**")) {
this.customizeResourceHandlerRegistration(registry.addResourceHandler(new String[]{"/webjars/**"}).
addResourceLocations(new String[]{"classpath:/META-INF/resources/webjars/"}).setCachePeriod(this.getSeconds(cachePeriod)).setCacheControl(cacheControl));
}
<!-- 静态资源映射规则-->
String staticPathPattern = this.mvcProperties.getStaticPathPattern();
if (!registry.hasMappingForPattern(staticPathPattern)) {
this.customizeResourceHandlerRegistration(registry.addResourceHandler(new String[]{staticPathPattern}).
addResourceLocations(getResourceLocations(this.resourceProperties.getStaticLocations())).
setCachePeriod(this.getSeconds(cachePeriod)).setCacheControl(cacheControl));
}
}
}
第一种webjars加载:所有 /webjars/** ,都去 classpath:/META-INF/resources/webjars/ 找资源;
第二种静态资源映射:
staticPathPattern 点击查看源码:staticPathPattern = /**;
resourceProperties.getStaticLocations() 点击查看源码:
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();
}
public String[] getStaticLocations() {
return this.staticLocations;
}
通过以上源码看出,访问资源路径跟一开始测试时的一样,没问题,为何通过配置设置登录页面就无法访问静态资源了呢,反复查看WebMvcAutoConfiguration源码发现如下:
@Configuration
@ConditionalOnWebApplication(type = Type.SERVLET)
@ConditionalOnClass({Servlet.class, DispatcherServlet.class, WebMvcConfigurer.class})
@ConditionalOnMissingBean({WebMvcConfigurationSupport.class})
@AutoConfigureOrder(-2147483638)
@AutoConfigureAfter({DispatcherServletAutoConfiguration.class, TaskExecutionAutoConfiguration.class, ValidationAutoConfiguration.class})
public class WebMvcAutoConfiguration {=
@ConditionalOnClass 和 @ConditionalOnMissingBean 这两个注解的条件成立WebMvcAutoConfiguration才会自动加载,
在配置登录页的时候,正好继承了WebMvcConfigurationSupport类 @ConditionalOnMissingBean({WebMvcConfigurationSupport.class}) 容器没有加载这个bean时自动加载WebMvcAutoConfiguration,
继承WebMvcAutoConfiguration的方式也可以,不过需要手动添加资源映射路径,WebMvcAutoConfiguration中的其他自动加载项也需要自己处理。 @ConditionalOnClass({Servlet.class, DispatcherServlet.class, WebMvcConfigurer.class}) 容器加载这个bean时自动加载WebMvcAutoConfiguration
重新修改配置类 实现WebMvcConfigurer接口对登录页进行处理,静态资源访问正常。
springboot 2.X 在访问静态资源的的时候出现404的问题的更多相关文章
- SpringBoot学习5:访问静态资源
springboot默认从项目的resources里面的static目录下或者webapp目录下访问静态资源 方式一:在resources下新建static文件(文件名必须是static) 在浏览器中 ...
- springboot(二 如何访问静态资源和使用模板引擎,以及 全局异常捕获)
在我们开发Web应用的时候,需要引用大量的js.css.图片等静态资源. 默认配置 Spring Boot默认提供静态资源目录位置需置于classpath下,目录名需符合如下规则: /static / ...
- Springboot访问静态资源&WebJars&图标&欢迎页面
目录 概述 1.访问WebJar资源 2.访问静态资源 3.favicon.ico图标 4.欢迎页面 概述 使用Springboot进行web开发时,boot底层实际用的就是springmvc,项目中 ...
- springBoot怎样访问静态资源?+静态资源简介
1.静态资源 怎样通过浏览器访问静态资源? 注意:不需要加static目录.因为只是告诉springboot目录,而不是静态资源路劲. 这时访问路径就需要加上/static
- SpringBoot: 5.访问静态资源(转)
springboot默认从项目的resources里面的static目录下或者webapp目录下访问静态资源 方式一:在resources下新建static文件(文件名必须是static) 在浏览器中 ...
- 解决SpringBoot页面跳转无法访问静态资源的问题
初学SpringBoot,写项目的时候遇到了问题,原本的页面是这样的 但启动项目后是这样的 这是因为thymeleaf中引入静态资源及模板需要使用到 th:xxx 属性,否则无法在动态资源中访问静态资 ...
- 【SpringBoot】06.SpringBoot访问静态资源
SpringBoot访问静态资源 1.SpringBoot从classpath/static的目录 目录名称必须是static 启动项目,访问http://localhost:8080/0101.jp ...
- springboot无法访问静态资源
无法访问static下的静态资源 1.在application.yml中添加 resources: static-locations: classpath:/META-INF/resources/,c ...
- springboot+themeleaf+bootstrap访问静态资源/无法访问静态资源/图片
在网页HTML上访问静态资源的正确写法例: 1.<img src="../../static/bootstarp/img/2.jpg" th:src="@{ ...
随机推荐
- mysql升级到5.7时间戳(timestamp)默认值报错
原文:mysql升级到5.7时间戳报错 往数据库里创建新表的时候报错: [Err] 1067 - Invalid default value for 'updateTime' DROP TABLE I ...
- Python 标准库 —— string
1. maketrans()/translate() maketrans(frm, to) -> string, 建立从字符串 frm 到 to 的映射表(字符串的形式): translate( ...
- uwp - 禁用屏幕翻转/禁用屏幕旋转/禁用横屏模式
原文:uwp - 禁用屏幕翻转/禁用屏幕旋转/禁用横屏模式 解决方案目录 > Package.appxmanifest 双击打开,把支持的旋转:纵向勾上,只勾这一个其他不勾,就可以了.同理,想让 ...
- SpringBoot、Groovy
Java——搭建自己的RESTful API服务器(SpringBoot.Groovy) 这又是一篇JavaWeb相关的博客,内容涉及: SpringBoot:微框架,提供快速构建服务的功能 Sp ...
- 新浪微博Python3客户端接口OAuth2
Keyword: Python3 Oauth2 新浪微博 本接口基于廖雪峰的weibo python SDK修改完成,其sdk为新浪官方所推荐,原作者是用python2写的 经过一些修改,这里提供基于 ...
- Asp.net-MyFirstMVCProject详细解释
一个URL要求, ASP.NET MVC引擎将分析URL要使用Controller, 这个Controller(取而代之的是,真实的方法Controller的Action)从数据库或者其它数据源获取数 ...
- LeetCode 36 Sudoku Solver
Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...
- asp .net mvc 获得用户IP
string strHostName = System.Net.Dns.GetHostName(); //clientIPAddress是一个数组,可能有多个数据 var clientIPAddres ...
- PHP获得指定日期所在月的第一天和最后一天
function getdays($day){ $firstday = date('Y-m-01',strtotime($day)); $lastday = date('Y-m-d',strtotim ...
- liunx 桥接 上网 ip配置 外部网络访问
一.设置VMware 在vmware的[编辑]-->[虚拟网络编辑器]设置:将VMnet0设置为“桥接”,并桥接到宿主机器的网卡(可以是有线或者无线网络). 二.设置虚拟机系统(以cento ...