一、场景
环境:
二、存在问题
三、解决方案
四、实战 - Swagger 2 升至 3.0 (Open API 3.0)
Stage 1:引入Maven依赖
Stage 2:Swagger 配置类
Stage 3:访问 Swagger 3.0
Stage 4:获取 json,保存为 swagger.json
Stage 5:showdoc 中导入 swagger.json
Stage 6:导入效果
一、场景
-
公司需要将 swagger-ui 中的接口导入到 showdoc 中
-
思路 :获取 swagger.json 并导入到 showdoc 中 (http://ip:port/v2/api-docs 获取json)
环境:
-
Spring boot : 2.5.4
-
Swagger ui : 2.9.2
-
JDK : 1.8
二、存在问题
-
http://ip:port/v2/api-docs 报错无法获取完整的JSON
三、解决方案
-
方案一:升至Swagger 3,获取json,保存为xxx.json,再导入 showdoc中 (适合全量导入)
-
方案二:集成 knife4j ,单个接口json,保存为xxx.json,再导入 showdoc中(适合部分导入 + 前端调试)
四、实战 - Swagger 2 升至 3.0 (Open API 3.0)
Stage 1:引入Maven依赖
<!--swagger3--><dependency><groupId>io.springfox</groupId><artifactId>springfox-boot-starter</artifactId><version>3.0.0</version></dependency>
Stage 2:Swagger 配置类
-
注意: .title("XPH- iot 接口文档")
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;// 自定义swagger3文档信息
@Configuration
@ConditionalOnProperty(value = "springfox.documentation.enabled", havingValue = "true", matchIfMissing = true)
@Slf4j
public class Swagger3Config extends WebMvcConfigurationSupport {@Value(value = "${host:localhost}")private String host;@Value("${server.port:8005}")private String port;@Beanpublic Docket createRestApi() {return new Docket(DocumentationType.OAS_30).apiInfo(apiInfo()).select().apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class)).paths(PathSelectors.any()).build();}private ApiInfo apiInfo() {log.info("http://{}:{}/swagger-ui.html", host, port);log.info("http://localhost:{}/swagger-ui.html", port);log.info("http://localhost:{}/swagger-ui/index.html", port);log.info("http://localhost:{}/doc.html", port);return new ApiInfoBuilder().title("XPH- iot 接口文档").description("更多请咨询").contact(new Contact("lijiong", "https://", "xxx@")).version("1.0.0").build();}@Overridepublic void addResourceHandlers(ResourceHandlerRegistry registry) {registry.addResourceHandler("/**").addResourceLocations("classpath:/static/");//swagger 2registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");//swagger 3registry.addResourceHandler("/swagger-ui/**").addResourceLocations("classpath:/META-INF/resources/webjars/springfox-swagger-ui/");registry.addResourceHandler("doc.html").addResourceLocations("classpath:/META-INF/resources/");registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");super.addResourceHandlers(registry);}
}
Stage 3:访问 Swagger 3.0
-
https://localhost:port/swagger-ui/index.html
Stage 4:获取 json,保存为 swagger.json
Stage 5:showdoc 中导入 swagger.json
-
全量导入,会自动生成一个项目名为:swaggerUI title的项目
Stage 6:导入效果