【問題】
使能了 @EnableSwagger2 ,檢查各處都沒有問題。
但訪問http://localhost:19900/swagger-ui.html#/
的時候始終報404。
用同樣方法在其他微服務中都沒有問題。
【問題原因】
猜想:可能是上傳文件的傳參和swagger原有生成機制衝突
【解決方案】
1、在Application中使能@EnableWebMvc
@EnableWebMvc
@EnableSwagger2
public class FileUploadApplication {
public static void main(String[] args) {
SpringApplication.run(FileUploadApplication.class,args);
}
}
2、添加WebConfig文件
package com.jingfatech.upload.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
/**
* webmvc 打開swagger
*
* @author ppmicro
* @date 2020/6/20 11:40
*/
@Configuration
public class WebConfig extends WebMvcConfigurationSupport {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/**").addResourceLocations(
"classpath:/static/");
registry.addResourceHandler("swagger-ui.html").addResourceLocations(
"classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**").addResourceLocations(
"classpath:/META-INF/resources/webjars/");
super.addResourceHandlers(registry);
}
}
做了上面兩步,問題解決。