21.1 为什么会有 Swagger


前后端分离的主流:Vue + SpringBoot

  • 后端:数据访问层、服务层、控制层【后端团队】
  • 前端:控制层、视图层【前端团队】

前后端如何进行交互: API 接口(url 请求)这个东西是后端写好的,我们前端直接 发送请求,然后拿到 Json 数据即可。

前后端分离目前的问题:集成联调的时候,前后端人员无法做到及时的协商。最好是 后续成本可能大的需求今早解决。以免最后导致问题集中爆发。

解决方案:

  • 首先 指定 Schema(计划),实时更新 最新的 API 接口。前后端可以第一时间看到。降低集成的风险。
  • 早些年的时候,指定 Word 计划 文档。前端测试后端接口 可以用 postman、后端提供接口,必须保持实时更新最新。

Swagger:号称最流行的 API 框架,它是 RestFul 风格的 API。并且 文档还 支持在线自动的生成。API 文档 与 API 定义 能同步更新。可以直接 运行,甚至可以在线测试 API 接口,还支持多种的编程语言。


21.2 SpringBoot 集成 Swagger

  1. 导入 相关 依赖
        <!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 --><dependency><groupId>io.springfox</groupId><artifactId>springfox-swagger2</artifactId><version>2.9.2</version></dependency><!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui --><dependency><groupId>io.springfox</groupId><artifactId>springfox-swagger-ui</artifactId><version>2.9.2</version></dependency>
  1. 写 swagger 的 config 配置类,先默认 不写任何的配置代码
package top.muquanyu.config;import org.springframework.context.annotation.Configuration;
import springfox.documentation.swagger2.annotations.EnableSwagger2;@Configuration
@EnableSwagger2
public class SwaggerConfig {// 很多的 配置 如果 你不配,那么用的全是 默认值}

如果 运行 报错。则需要 去 application 配置中,配置一下。

spring:mvc:pathmatch:matching-strategy: ant_path_matcher

问题出现的原因是:Spring Boot 2.6.X使用PathPatternMatcher匹配路径,而 Swagger 使用的路径匹配是基于 AntPathMatcher 的。上述的那个方法 只能 治标,但不治本。

把下面的 这个 方法 写到 配置类里面,即可 治本。只不过 这个 治本的方法 是 适用在 swagger 3.0.0 版本上的。

@Bean
public static BeanPostProcessor springfoxHandlerProviderBeanPostProcessor() {return new BeanPostProcessor() {@Overridepublic Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {if (bean instanceof WebMvcRequestHandlerProvider || bean instanceof WebFluxRequestHandlerProvider) {customizeSpringfoxHandlerMappings(getHandlerMappings(bean));}return bean;}private <T extends RequestMappingInfoHandlerMapping> void customizeSpringfoxHandlerMappings(List<T> mappings) {List<T> copy = mappings.stream().filter(mapping -> mapping.getPatternParser() == null).collect(Collectors.toList());mappings.clear();mappings.addAll(copy);}@SuppressWarnings("unchecked")private List<RequestMappingInfoHandlerMapping> getHandlerMappings(Object bean) {try {Field field = ReflectionUtils.findField(bean.getClass(), "handlerMappings");field.setAccessible(true);return (List<RequestMappingInfoHandlerMapping>) field.get(bean);} catch (IllegalArgumentException | IllegalAccessException e) {throw new IllegalStateException(e);}}};
}

如果 没报错,那就啥也别 弄,啥也别改。这么地 就行了。

  1. 直接访问 http://localhost:8080/swagger-ui.html


4. 简单的配置 Swagger

Docket 是 Swagger 的 核心 实例。

我们去 读一下 他的 源码。

我们 还发现 它 有 很多 待设置的 属性。

  • AppInfo



也就是说 它 需要 一个 实体的对象。AppInfo


你会发现 AppInfo 也有很多 需要设置的属性。

package top.muquanyu.config;import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;import java.util.ArrayList;@Configuration
@EnableSwagger2
public class SwaggerConfig {// 很多的 配置 如果 你不配,那么用的全是 默认值@Beanpublic Docket docket(){return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo());}private ApiInfo apiInfo(){Contact contact = new Contact("作者的名字","作者的网址","作者的email");// 作者信息return new ApiInfo("API文档的名字","API文档的描述","版本号","组织的网站/网址",contact,"许可证","许可证的网址", new ArrayList<>());}
}

SpringBoot 21 Swagger 2.9.2相关推荐

  1. springboot整合swagger+knife4j

    springboot整合swagger+knife4j 参考网址: https://mp.weixin.qq.com/s/KlYj5JuJSJYQQ47mQu7b1w swagger配置参考文档 sw ...

  2. 商城+前后端分离+课程设计+大作业 Springboot+vue+swagger+好看的安卓界面

    这里写目录标题 一.绪论 二.需求分析 2.2商户与顾客间通讯服务 2.3 完整的购物流程 2.4 后台管理 2.5 交流社区 2.6 附加功能 2.7 部署在云服务器上 三.总体设计 3.1系统结构 ...

  3. springboot整合swagger+mybatisplus案例

    1.前后端分离的一个常用的文档接口swaggerui越来越受欢迎,方便了前端以及后端人员的测试 2.如下为springboot整合swagger和mybatispus案例的github地址:https ...

  4. 六、springboot整合swagger

    六.springboot整合swagger 简介 swagger 提供最强大,最易用的工具,以充分利用OpenAPI规范. 官网 : https://swagger.io/ 准备工作 pom.xml ...

  5. Springboot整合swagger指南

    Springboot整合swagger指南 1. 安装使用 1.1 下载依赖 <dependency><groupId>io.springfox</groupId> ...

  6. [Swagger2]SpringBoot集成Swagger

    SpringBoot集成Swagger 引入依赖 <!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 ...

  7. 解决高版本SpringBoot整合swagger时启动报错:Failed to start bean ‘documentationPluginsBootstrapper‘ 问题

    一.控制台的报错信息 2021-12-29 15:15:04 [main] ERROR org.springframework.boot.SpringApplication - Application ...

  8. Swagger自动接口文档生成框架————springboot整合swagger总结

    swagger简介: swagger是一款开源的api接口文档生成工具. Swagger的项目主页:https://swagger.io/    目前比较流行的做法是在代码中加入swagger相关的注 ...

  9. springboot整合swagger(高版本)异常

    springboot整合swagger(高版本)异常 参考文章: (1)springboot整合swagger(高版本)异常 (2)https://www.cnblogs.com/chbyiming- ...

最新文章

  1. Python小知识 | 这些技能你不会?(二)
  2. oracle快速了解法,【oracle】rownum的快速了解
  3. MATLAB 与 Excel 接口
  4. MySQL 5.6 for Windows 解压缩版配置安装
  5. 小白入门机器学习必备:编程语言环境介绍及搭建
  6. Json 与 JS对象的关系与转换
  7. 《构建之法》第四章读后感--软件工程
  8. 有效解决vue动态绑定多个class的官方实例语法无效的问题
  9. AMapLocationListener 高德地图定位监听
  10. 小程序实现列表和详情页
  11. 前端的学习之路:初级HTML---图片标签
  12. 五分钟了解机器学习的基本概念
  13. 服务器被入侵了怎么办?
  14. sit是什么环境_测试理论——SIT测试 和 UAT测试概念
  15. c++个人银行账户管理3
  16. Android DataBinding 详解
  17. 【ubuntu】The following signatures couldn‘t be verified because the public key is not available
  18. python给定一个整数n、判断n是否为素数_python判断所输入的任意一个正整数是否为素数的两种方法...
  19. 谈谈网络工程师的就业方向与薪资水平
  20. PHP curl的DNS解析问题(PHP下curl很慢)

热门文章

  1. 关于Windows Server 服务器 解决主机漏洞初略指南 (CVE-2015-1635 | CVE-2012-0002 | CVE-2012-0152 | CVE-2017-0144) 等
  2. 阿里P6面试题-转载
  3. 联想服务器pxe安装系统,PXE下无人值守配置阵列及安装CentOS7
  4. 电脑网络能用,但是无法访问网页问题
  5. 5MW风电永磁直驱发电机-1200V直流并网MATLAB仿真模型
  6. 串口传输数据卡死问题解决方案
  7. 李志诉腾讯《明日之子》侵权案获赔20万
  8. 517我要吃:20大城市的20道特色面食与特色美食小吃
  9. vscode open in browser配置默认浏览器
  10. 番茄学习法及番茄钟软件 Pomodairo 1.9