ReturnTmp's Blog ReturnTmp's Blog
首页
基础课程
编程语言
框架技术
运维笔记
人工智能
随笔摘录
  • 友链
关于
收藏
  • 分类
  • 标签
  • 归档
GitHub (opens new window)

ReturnTmp

分享有趣好玩的计算机知识
首页
基础课程
编程语言
框架技术
运维笔记
人工智能
随笔摘录
  • 友链
关于
收藏
  • 分类
  • 标签
  • 归档
GitHub (opens new window)
  • GLIBC_2.28 not found 问题解决
  • Monorepo 多项目单仓库
  • Renovate 第三方依赖更新监控
  • 【IDEA】Maven 构建项目生成文件解析
  • K8s

  • Zotero 使用指南
  • package-lock.json 是否提交问题
  • 『ARM』和『x86』处理器架构解析指南
  • GlassFish 安装配置
  • 手把手教你安装配置『Oracle Database 19c』
  • Oracle Database 19c 彻底卸载
  • 语雀故障回顾
  • OpenStack 云计算平台 Nova 计算服务学习指南
  • Vue devServer 教程
  • Swagger 导出 API 文档
  • MapStruct POJO 映射框架指南
  • IDEA 代码热部署和热加载
  • SpringBoot 启动参数配置
  • Nacos 入门指南
  • seleuim 指南
  • Spring 服务降级熔断
  • Maven BOM 解析
  • .vscode 文件夹
  • Spring Security Token 认证
  • SpringBoot 基于 Actuator 和 Admin 实现应用监控管理
  • SPM/SCM 流量跟踪体系
  • Netty 入门
  • Flyway 数据库版本管理实战指南
  • Swagger 2 和 3 安装区别
    • 前言
    • 2
    • 3
      • SpringFox
      • SpringDoc
    • knife4j
    • 参考链接
  • MP 配置分页
  • MySQL 分库分表
  • Git Commit 提交规范,变更日志、版本发布自动化和 Emoji 提交标准
  • VSCode 插件 i18n Ally 进行国家化配置
  • Vue3 组合式 全局挂载
  • TS 教程
  • 架构解析:同城双活、异地多活、单元化架构
  • Spring 跨域配置
  • SpringCloud 微服务实战
  • Sentinel 流量治理组件教程
  • leetcode 上分
  • JMeter 压测
  • Netty IM 系统
  • IDEA 插件开发
  • SpringBoot 邮件服务 集成配置 详解
  • Maven 依赖包冲突问题解决
  • 社区项目 forest 修改
  • Maven 项目命名规范
  • 新版 PyCharm 设置 Conda 虚拟环境
  • 框架工具
ReturnTmp
2023-11-27
目录

Swagger 2 和 3 安装区别

# 前言

官方首页:API Documentation & Design Tools for Teams | Swagger (opens new window)

# 2

swagger 2 文档地址:OpenAPI Specification - Version 2.0 | Swagger (opens new window)

其中 SpringBoot 版本不可过高

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.5.6</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
1
2
3
4
5
6
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.9.2</version>
        </dependency>

        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.9.2</version>
        </dependency>
1
2
3
4
5
6
7
8
9
10
11
@Configuration  
@EnableSwagger2  
public class Swagger2Configuration {  
  
    /**  
     * 配置 Swagger 2  
     * 注册一个 Bean 属性  
     * enable():是否启用 Swagger,启用后才能在浏览器中进行访问  
     * groupName():用于配置 API 文档的分组  
     */  
    @Bean  
    public Docket docket() {  
        return new Docket(DocumentationType.SWAGGER_2)  
                .apiInfo(apiInfo())  
                .enable(true)  
                .groupName("v1")  
                .select()  
                // 过滤路径  
                //.paths(PathSelectors.ant())  
                // 指定扫描的包  
                .apis(RequestHandlerSelectors.basePackage("com.example.compkeylab.controller"))  
                .build();  
    }  
  
    private ApiInfo apiInfo() {  
        return new ApiInfoBuilder()  
                .title("电商实验")  
                .description("测试接口文档")  
                //.termsOfServiceUrl("SSS")  
                //.contact(new Contact("SSS", "SSS", "SSS"))                .version("Apache 2.0")  
                .build();  
    }  
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33

访问地址: http://localhost:8080/swagger-ui.html

# 3

swagger 3 文档地址:OpenAPI Specification - Version 3.0.3 | Swagger (opens new window)

导入 swagger 3 有两种方式:SpringFox 工具(旧),SpringDoc 工具(新)

# SpringFox

    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-boot-starter</artifactId>
        <version>3.0.0</version>
    </dependency>
1
2
3
4
5
/**
 * swagger3 配置
 */
@Configuration
public class Swagger3Configuration {

    Boolean swaggerEnabled = true;

    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.OAS_30)
                .apiInfo(apiInfo())
                // 是否开启
                .enable(swaggerEnabled)//true
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.example.compkeylab"))
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("电商实验")
                //创建人
                .contact(new Contact("sss", "http://www.baidu.com", "sss@x.com"))
                .version("1.0")
                .description("描述信息")
                .build();
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30

如果是 SpringBoot 版本为 2.6 或是出现问题 Failed to start bean 'documentationPluginsBootstrapper',请添加如下到 yaml

spring:
  mvc:
    pathmatch:
      matching-strategy: ANT_PATH_MATCHER
1
2
3
4

还有就是如果 swagger 3 移除 swagger 2 中自带的 guava 依赖,如果您的项目中使用了 guava,那么在迁移 swagger 3 之后需要导入 guava 依赖

<!-- https://mvnrepository.com/artifact/com.google.guava/guava -->
<dependency>
    <groupId>com.google.guava</groupId>
    <artifactId>guava</artifactId>
    <version>28.0-jre</version>
</dependency>
1
2
3
4
5
6

访问地址: http://localhost:8080/swagger-ui/index.html

# SpringDoc

暂时不使用

# knife4j

使用 knife4j 优化接口文档

<dependency>  
    <groupId>com.github.xiaoymin</groupId>  
    <artifactId>knife4j-spring-boot-starter</artifactId>  
    <version>3.0.2</version>  
</dependency>
1
2
3
4
5

然后直接访问: http://localhost:8080/doc.html

配置类忽略

# 参考链接

  • SpringBoot集成Swagger3.0(详细) - 蚂蚁小哥 - 博客园 (cnblogs.com) (opens new window)
  • 秒懂SpringBoot之如何集成SpringDoc(全网目前最新最系统最全面的springdoc教程) - ShuSheng007 (opens new window)
  • spring boot集成swagger3 - 路迢迢 - 博客园 (cnblogs.com) (opens new window)
  • SpringBoot集成Swagger3 —— 教你如何优雅的写文档_律二萌萌哒的博客-CSDN博客 (opens new window)
编辑 (opens new window)
上次更新: 2023/11/28, 12:55:10
Flyway 数据库版本管理实战指南
MP 配置分页

← Flyway 数据库版本管理实战指南 MP 配置分页→

最近更新
01
百度网盘加速
03-24
02
新版 PyCharm 设置 Conda 虚拟环境
03-24
03
腾讯云域名转到阿里云
03-24
更多文章>
Theme by Vdoing | Copyright © 2023-2024 ReturnTmp | MIT License
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式