使用SpringBoot1.5.3整合Swagger2(2.6.1)時遇到如下問題:
由于需要添加Mvc Interceptor
自定義了WebMvcConfig extends WebMvcConfigurationSupport
重寫addResourceHandlers,addInterceptors方法.
在addResourceHandlers方法中代碼如下:
String RESOURCE_LOCATION = "classpath:/META-INF/resources/";
registry.addResourceHandler("/swagger-ui.html").addResourceLocations(RESOURCE_LOCATION);
registry.addResourceHandler("/webjars/**").addResourceLocations(RESOURCE_LOCATION + "/webjars/");// 1
registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");```
以上1處代碼存在問題。如果將打包成的war直接部署到tomcat中,訪問Swagger沒有問題。
但是如果使用java -jar xxx.war(使用的embeed tomcat) 方式則無法訪問。因為RESOURCE_LOCATION 變量已“/”結(jié)尾,webjars前又寫了一個"/",導(dǎo)致路徑錯誤。去掉webjars前面的"/"即可。