• 【日常总结】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依赖

    1. <dependency>
    2. <groupId>io.springfoxgroupId>
    3. <artifactId>springfox-boot-starterartifactId>
    4. <version>3.0.0version>
    5. dependency>
    wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==

    Stage 2:Swagger 配置类

    • 注意: .title("XPH- iot 接口文档") 

    1. import io.swagger.annotations.ApiOperation;
    2. import lombok.extern.slf4j.Slf4j;
    3. import org.springframework.beans.factory.annotation.Value;
    4. import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
    5. import org.springframework.context.annotation.Bean;
    6. import org.springframework.context.annotation.Configuration;
    7. import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
    8. import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
    9. import springfox.documentation.builders.ApiInfoBuilder;
    10. import springfox.documentation.builders.PathSelectors;
    11. import springfox.documentation.builders.RequestHandlerSelectors;
    12. import springfox.documentation.service.ApiInfo;
    13. import springfox.documentation.service.Contact;
    14. import springfox.documentation.spi.DocumentationType;
    15. import springfox.documentation.spring.web.plugins.Docket;
    16. // 自定义swagger3文档信息
    17. @Configuration
    18. @ConditionalOnProperty(value = "springfox.documentation.enabled", havingValue = "true", matchIfMissing = true)
    19. @Slf4j
    20. public class Swagger3Config extends WebMvcConfigurationSupport {
    21. @Value(value = "${host:localhost}")
    22. private String host;
    23. @Value("${server.port:8005}")
    24. private String port;
    25. @Bean
    26. public Docket createRestApi() {
    27. return new Docket(DocumentationType.OAS_30)
    28. .apiInfo(apiInfo())
    29. .select()
    30. .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
    31. .paths(PathSelectors.any())
    32. .build();
    33. }
    34. private ApiInfo apiInfo() {
    35. log.info("http://{}:{}/swagger-ui.html", host, port);
    36. log.info("http://localhost:{}/swagger-ui.html", port);
    37. log.info("http://localhost:{}/swagger-ui/index.html", port);
    38. log.info("http://localhost:{}/doc.html", port);
    39. return new ApiInfoBuilder()
    40. .title("XPH- iot 接口文档")
    41. .description("更多请咨询")
    42. .contact(new Contact("lijiong", "https://", "xxx@"))
    43. .version("1.0.0")
    44. .build();
    45. }
    46. @Override
    47. public void addResourceHandlers(ResourceHandlerRegistry registry) {
    48. registry.addResourceHandler("/**").addResourceLocations("classpath:/static/");
    49. //swagger 2
    50. registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");
    51. //swagger 3
    52. registry.addResourceHandler("/swagger-ui/**").addResourceLocations("classpath:/META-INF/resources/webjars/springfox-swagger-ui/");
    53. registry.addResourceHandler("doc.html").addResourceLocations("classpath:/META-INF/resources/");
    54. registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
    55. super.addResourceHandlers(registry);
    56. }
    57. }
    wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==

    Stage 3:访问 Swagger 3.0

     

    Stage 4:获取 json,保存为 swagger.json

      

     

    Stage 5:showdoc 中导入 swagger.json

    • 全量导入会自动生成一个项目名为:swaggerUI title的项目

     

     

    Stage 6:导入效果

     

  • 相关阅读:
    P5661 [CSP-J2019] 公交换乘
    Kafka无法对外暴露端口的相关解决方案
    【TypeScript笔记】02 - TS高级类型
    好多自恋性数
    【华为云云耀云服务器L实例评测】- 云原生实践,快捷部署人才招聘平台容器化技术方案!
    postgres源码解析 SysLogger辅助进程
    带你刷(牛客网)C语言百题(第四天)
    Python 的列表
    《Web安全基础》07. 反序列化漏洞
    asp.net core automapper的使用
  • 原文地址:https://blog.csdn.net/ladymorgana/article/details/134538815