- 在SpringBoot项目中,想使用(localhost:端口号/资源根路径)访问项目目录下的某一带有中文、特殊字符的资源时,报404:
-
访问纯英文的资源时没问题
-
原因在于SpringBoot2.6版本之后修改了映射请求处理的默认策略,由原来的AntPathMatcher更改为PathPatternParser
- 解决方法1:
在WebMvcConfig中关闭decode
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
/*更改程序映射请求路径默认策略*/
@Override
public void configurePathMatch(PathMatchConfigurer configurer) {
UrlPathHelper urlPathHelper=new UrlPathHelper();
urlPathHelper.setUrlDecode(false);
urlPathHelper.setDefaultEncoding(StandardCharsets.UTF_8.name());
configurer.setUrlPathHelper(urlPathHelper);
}
}
- 解决方法2:
在properties或yaml中,更改springboot2.6之后的默认程序映射请求策略
spring.mvc.pathmatch.matching-strategy=ant_path_matcher