springBoot静态资源路径映射配置不生效,浏览器访问为404(addResourceHandler,addResourceLocations springboot-2.6.x不生效)的可能原因

在SpringBoot 2.6.7版本中,静态资源路径映射配置如`addResourceHandler`和`addResourceLocations`不生效导致404错误。原因是Spring MVC的路径匹配策略从AntPathMatcher更改为PathPatternParser。解决办法包括:降级SpringBoot版本至2.5.x,添加配置`spring.mvc.pathmatch.matching-strategy=ant-path-matcher`,或使用低版本spring-mvc依赖。
摘要由CSDN通过智能技术生成

springBoot静态资源路径映射配置不生效(addResourceHandler,addResourceLocations springboot-2.6.x不生效)的可能原因

静态资源路径映射配置的大致代码如下:

@Configuration
public class WebMvcConfig implements WebMvcConfigurer _{
_private static String localhostPath = “D:\学习资料\学习笔记\springboot+vue+elementUI-个人博客\images\”;

@Override
public void addResourceHandlers_(ResourceHandlerRegistry registry) {_
// registry.addResourceHandler(“虚拟路径”).addResourceLocations(“file:本地资源路径”);
registry.addResourceHandler_(“/image/**”).addResourceLocations(_“file:” + localhostPath);

}
}

随后发现配置不生效,使用浏览器打开都是404,已确认addResourceHandler和addResourceLocations所填的值均是正确的

当前使用的springboot版本是2.6.7版本

随后将springboot依赖修改成2.5.8版本后,静态资源映射就生效了

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值