整合WebMvcConfigurationSupport後,swagger3.0頁面404無法訪問
阿新 • • 發佈:2022-05-07
1. 問題描述
- springboot工程,訪問伺服器靜態資原始檔,所以添加了配置類繼承WebMvcConfigurationSupport
@Slf4j @Configuration public class WebConfig extends WebMvcConfigurationSupport { @Autowired ComponentDirectoryPathReader pathReader; @Override protected void addResourceHandlers(ResourceHandlerRegistry registry) {String filePath = "C:\\Users\\Administrator\\Desktop"; log.info("filePath:"+filePath); registry.addResourceHandler("/**"). addResourceLocations("classpath:/static/").addResourceLocations("classpath:META-INF/resources/"). addResourceLocations("file:"+filePath);} }
- 訪問swagger頁面404
2.排查思路
檢視swagger的配置
- SwaggerUiWebMvcConfigurer 中配置了swagger靜態資源訪問路徑,所以將其合併到自己的配置類中即可
public class SwaggerUiWebMvcConfigurer implements WebMvcConfigurer { private final String baseUrl; public SwaggerUiWebMvcConfigurer(String baseUrl) { this.baseUrl = baseUrl; }public void addResourceHandlers(ResourceHandlerRegistry registry) { String baseUrl = StringUtils.trimTrailingCharacter(this.baseUrl, '/'); registry.addResourceHandler(new String[]{baseUrl + "/swagger-ui/**"}).addResourceLocations(new String[]{"classpath:/META-INF/resources/webjars/springfox-swagger-ui/"}).resourceChain(false); } public void addViewControllers(ViewControllerRegistry registry) { registry.addViewController(this.baseUrl + "/swagger-ui/").setViewName("forward:" + this.baseUrl + "/swagger-ui/index.html"); } }
3.解決方案
@Slf4j @Configuration public class WebConfig extends WebMvcConfigurationSupport { @Autowired ComponentDirectoryPathReader pathReader; @Override protected void addResourceHandlers(ResourceHandlerRegistry registry) { String filePath = "C:\\Users\\Administrator\\Desktop"; log.info("filePath:"+filePath); registry.addResourceHandler("/**"). addResourceLocations("classpath:/static/").addResourceLocations("classpath:META-INF/resources/"). addResourceLocations("file:"+filePath); registry.addResourceHandler("/swagger-ui/**").addResourceLocations("classpath:/META-INF/resources/webjars/springfox-swagger-ui/"); } }