问题:
今天接手一个项目,明明配置了Swagger
,也开启了,但就是访问不到页面,spring
指定spring-pro
配置文件就可以访问,指定spring-dev
文件就不行,难不成spring-pro
配置文件里有开关???
错误信息:
Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Tue Mar 12 17:21:01 CST 2024
There was an unexpected error (type=Not Found, status=404).
No message available
原因分析:
那一定是某行代码有问题,我仔细比对后发现
spring-dev
配置文件比spring-pro
配置文件多了如下代码:
mvc:
# 静态地址路径
static-path-pattern: /public/**
static-path-pattern
这一路径无法设置多个参数,原因如下:
源码文件名:WebMvcProperties.java
# 源码中,接收此方法的参数就写死了,是单值传递,只能传一个,并不是数组。
public void setStaticPathPattern(String staticPathPattern) {
this.staticPathPattern = staticPathPattern;
}
# 这是源码中设置的默认值
public WebMvcProperties() {
this.localeResolver = WebMvcProperties.LocaleResolver.ACCEPT_HEADER;
this.dispatchTraceRequest = false;
this.dispatchOptionsRequest = true;
this.ignoreDefaultModelOnRedirect = true;
this.publishRequestHandledEvents = true;
this.throwExceptionIfNoHandlerFound = false;
this.logResolvedException = false;
this.staticPathPattern = "/**"; # 默认值在这
this.async = new Async();
this.servlet = new Servlet();
this.view = new View();
this.contentnegotiation = new Contentnegotiation();
this.pathmatch = new Pathmatch();
}
把此行注释掉后,重新启动项目,Swagger-ui
就能访问到了。
解决方法
注释掉spring
配置文件中mvc
下的静态路径地址即可。