当前位置: 首页 > news >正文

【日常总结】Swagger-ui 导入 showdoc (优雅升级Swagger 2 升至 3.0)

一、场景

环境:

二、存在问题

三、解决方案

四、实战 - 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 ui2.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>
wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==

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);}
}
wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==

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:导入效果

 

http://www.lryc.cn/news/238049.html

相关文章:

  • OpenCV C++ 图像 批处理 (批量调整尺寸、批量重命名)
  • RT-DETR手把手教程,注意力机制如何添加在网络的不同位置进行创新优化
  • qt treeview 删除节点
  • 【单词】【2019】
  • Java自动化驱动浏览器搜索稻香
  • php聊天室通讯系统常用的接口对接函数 curl、file_get_contents()、WebSocket、消息队列
  • SQL基础理论篇(九):存储过程
  • 申银万国期货通过ZStack Cube信创超融合一体机打造金融信创平台
  • SquareCTF-2023 Web Writeups
  • Docker-compose 安装mysql8
  • 分布式锁实现对比
  • Ubuntu 系统上使用 QQ 邮箱的 SMTP 服务器发送邮件,msmtp(已验证)
  • 笔记54:门控循环单元 GRU
  • 数据仓库高级面试题
  • 【OpenGauss源码学习 —— 列存储(ColumnTableSample)】
  • 【开源】基于JAVA的校园二手交易系统
  • C 语言结构体(struct)
  • Linux:zip包的压缩与解压
  • Linux 时区设置
  • Linux本地WBO创作白板部署与远程访问
  • leetcode刷题日记:205. Isomorphic Strings(同构字符串)
  • Autox.js和Auto.js4.1.1手机编辑器不好用我自己写了一个编辑器
  • docker logs 如何使用grep检索
  • 【教3妹学编辑-mysql】详解join(内连接、外连接、交叉连接等)
  • 云工作流 CloudFlow 重磅发布,流程式开发让云上应用构建更简单
  • 基于单片机GPS轨迹定位和里程统计系统
  • go 适配器模式
  • 蓝桥杯物联网_STM32L071_1_CubMxkeil5基础配置
  • 如果文件已经存在与git本地库中,配置gitignore能否将其从git库中删除
  • 枚举 小蓝的漆房