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

后端整合Swagger+Knife4j接口文档

后端整合Swagger+Knife4j接口文档

接口文档介绍

  1. 什么是接口文档:写接口信息的文档,条接口包括:
    • 请求参数
    • 响应参数
      • 错误码
    • 接口地址
    • 接口名称
    • 请求类型
    • 请求格式
    • 备注
  2. 为什么需要接口文档
    • who用?后端提供,前后端都需要使用
    • 有一个书面归档,便于参考和查阅,沉淀和维护
    • 便于前端和后端的开发,联调的介质。后端=>接口文档<=前端
    • 在线测试,作为工具,提高开发速率
  3. 怎么做接口文档
    • 手写(比如腾讯笔记,markdown笔记)
    • 自动化接口文档生成:根据项目代码生成完整的文档或在线联调工具Swagger,Postman(侧重接管理),apifox,apipost,eolink
  4. Swagger接口的原理
    • 自定义Swagger配置类
    • 定义需要生成接口文档的代码位置(controller)

项目中使用Swagger+Knife4j接口文档

  1. Swagger官方:https://swagger.io/
  2. 项目中引入依赖
    <groupId>io.springfox</groupId><artifactId>springfox-swagger2</artifactId><version>2.7.0</version></dependency><dependency><groupId>io.springfox</groupId><artifactId>springfox-swagger-ui</artifactId><version>2.7.0</version></dependency>
    
  3. 自定义Swagger配置类,在springBoot项目中使用springBoot的注解,生成一个swagger配置的bean。
    	package com.yupi.usercenter.config;import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    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;
    import springfox.documentation.swagger2.annotations.EnableSwagger2WebMvc;/*** 自定义swagger的配置*/
    @Configuration
    @EnableSwagger2WebMvc // Swagger的开关,表示已经启用Swagger
    public class SwaggerConfig {@Bean(value = "defaultApi2") //生成一个swagger的配置,框架扫描到这个配置,注入到swagger的对象中,就可以初始化一个文档public Docket createRestApi(){return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select()//标注控制器的位置.apis(RequestHandlerSelectors.basePackage("com.yupi.usercenter.controller")).paths(PathSelectors.any()).build();}/*** api信息* @return*/private ApiInfo apiInfo() {return new ApiInfoBuilder().title("用户中心").contact(new Contact("whale", "www.xxx.com", "xxx@qq.com")).description("这是用Swagger动态生成的用户中心接口文档").termsOfServiceUrl("NO terms of service").version("1.0").build();}
    }
    
    若springboot versio>=2.6,需要添加以下配置
      mvc:pathmatch:matching-strategy: ANT_PATH_MATCHER
    
  4. 结果展示在这里插入图片描述
http://www.lryc.cn/news/248242.html

相关文章:

  • k8s中批量处理Pod应用的Job和CronJob控制器介绍
  • UE5 范围内随机生成
  • 杂记 | 使用Docker安装并配置MongoDB以支持事务(单副本,并解决了证书文件错误的问题)
  • css三角,鼠标样式,溢出文字
  • 远程桌面访问MATLAB 2018B,提示License Manger Error -103,终极解决方案
  • Jmeter基础和概念
  • 【Linux 带宽限速】trickle,限制docker 上传速度
  • MindStudio学习记录三:推理应用开发 acl mindx sdk
  • 【RT-DETR改进】SIoU、GIoU、CIoU、DIoU、AlphaIoU等二十余种损失函数
  • 【Linux】EVIOCGBIT
  • 鸿蒙4.0开发笔记之ArkTS装饰器语法基础@Extend扩展组件样式与stateStyles多态样式(十一)
  • 5V摄像机镜头驱动IC GC6208,可用于摄像机,机器人等产品中可替代AN41908
  • PHP echo和print 语句
  • ThinkPHP6.1 多应用模式的一些事儿
  • redis-cluster集群模式
  • 带你用uniapp从零开发一个仿小米商场_10. 首页开发
  • 常使用的定时任务
  • 【人工智能Ⅰ】实验2:遗传算法
  • Hadoop集群升级(3.1.3 -> 3.2.4)
  • (一)基于高尔夫优化算法GOA求解无人机三维路径规划研究(MATLAB)
  • ESP32-Web-Server编程-建立第一个网页
  • csgo/steam游戏搬砖项目的五大认知误区
  • ASCII sorting
  • redis--高可用之持久化
  • 『VUE3 の 要点摘录』
  • Buzz库python代码示例
  • Django路由分发
  • 【UGUI】中Content Size Fitter)组件-使 UI 元素适应其内容的大小
  • 第二十章Java博客
  • 实验一 SAS 基本操作和数据表的导入 2023-11-29