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

全网没有之一的API 文档:Swagger

 【文章末尾给大家留下了大量的福利】

Swagger 介绍

Swagger UI 允许任何人(无论是开发团队还是最终用户)都可以可视化 API 资源并与之交互,而无需任何实现逻辑。

Swagger API 文档是根据 OpenAPI(以前称为 Swagger)规范自动生成的,可简化后端实现和客户端的使用。

Swagger 依赖

注意:Swagger 与 SpringBoot 高版本(2.6.x)不兼容(启动报错),因此需使用低版本(如 2.3.5)的 SpringBoot 。

    <!-- 兼容 Swagger 的 SpringBoot 版本 -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.5.RELEASE</version>
        <relativePath/>
    </parent>
    ...
        <!-- Swagger 相关依赖 -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.9.2</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-bean-validators</artifactId>
            <version>2.8.0</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.9.2</version>
        </dependency>
        <dependency>
            <groupId>io.swagger</groupId>
            <artifactId>swagger-annotations</artifactId>
            <version>1.5.21</version>
        </dependency>
        <dependency>
            <groupId>io.swagger</groupId>
            <artifactId>swagger-models</artifactId>
            <version>1.5.21</version>
        </dependency>

Swagger 配置类(SpringBoot)

package com.example.apitestplatform.config;

import com.google.common.collect.Lists;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
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;

/**
 * Swagger 文档配置类
 */
@Configuration
@EnableSwagger2
public class SwaggerConfig {

    @Bean
    public Docket docket() {
        ParameterBuilder builder=new ParameterBuilder();
        builder.parameterType("header")
                .name("token")
                .description("token值")
                .required(true)
                .modelRef(new ModelRef("string"));  // 在swagger文档里展示header

        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("ApiDemo")
                .apiInfo(apiInfo())
                .globalOperationParameters(Lists.newArrayList(builder.build()))
                .select()  // 选择生成策略
                .apis(RequestHandlerSelectors.basePackage("com.example.apitestplatform.controller"))  // 选择生成文档的类(忽略该行则不做过滤)
                .paths(PathSelectors.any())
                .build();
    }

    // 定义接口文档基本信息
    private ApiInfo apiInfo(){
        return new ApiInfoBuilder()
                .title("ApiDemo 系统")  // 接口文档标题
                .description("ApiDemo 接口文档")  // 接口文档描述
                .contact(new Contact("xiaoming", "", "103@qq.com"))  // 作者联系方式
                .version("1.0")  // 接口文档版本
                .build();
    }
}

Swagger 常用注解

  • @Api(tags="API 类标题")
  • @ApiOperation(value="API 方法标题")
  • @ApiModel(value="实体类标题", description="实体类描述")
  • @ApiModelProperty(value="实体属性描述",example="属性示例取值", required=true)

示例:

  • 接口类:
package com.example.apitestplatform.controller;

import com.example.apitestplatform.entity.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

@Api(tags="Swagger Demo 类")
@RestController
@RequestMapping(value="demo")
public class DemoController {

    @ApiOperation(value="Swagger Demo get方法")
    @GetMapping("loginGet")
    public String loginGet() {
        return "登录成功";
    }

}
  • User 类
package com.example.apitestplatform.entity;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

@ApiModel(value="用户类", description="用户信息")
@Data
public class User {

    @ApiModelProperty(value="用户名", example="xiaoming", required=true)
    private String username;
    @ApiModelProperty(value="密码", example="123456", required=true)
    private String password;
}

效果示例

 重点:学习资料学习当然离不开资料,这里当然也给你们准备了600G的学习资料

需要的先关注再私我关键字【000】免费获取哦 注意关键字是:000

疑惑:为什么要先关注呢?   回:因为没关注的话私信回了你看不到

项目实战

app项目,银行项目,医药项目,电商,金融

大型电商项目

全套软件测试自动化测试教学视频

300G教程资料下载【视频教程+PPT+项目源码】

全套软件测试自动化测试大厂面经

python自动化测试++全套模板+性能测试

听说关注我并三连的铁汁都已经升职加薪暴富了哦!!!!

相关文章:

  • dubbo环境搭建ZooKeeper注册中心
  • (附源码)ssm教师工作量核算统计系统 毕业设计 162307
  • 欧拉函数算法的实现
  • Ubuntu 创建本地 Git 并与 Github(私有库) 交互(上传与下载)| 记录 | 踩坑
  • 6.0、软件测试——判定表法
  • 面试题~~
  • 容斥原理算法的实现
  • 【爬虫】Python使用动态IP,多线程,爬取uncomtrade的数据
  • 【国科大——认知计算】认知计算 第一次研讨课
  • 20天深度复习JavaSE的详细笔记(十二)——集合(Collection、数据结构、List、泛型深入)
  • 测试项目中的风险管理
  • 数据结构第一课 —— 时间和空间复杂度
  • 剑指offer-62-圆圈中最后剩下的数字
  • 【vue】vue3中状态管理Pinia(Vuex5)使用快速上手
  • Java序列化有什么作用
  • [译] 理解数组在 PHP 内部的实现(给PHP开发者的PHP源码-第四部分)
  • 【翻译】Mashape是如何管理15000个API和微服务的(三)
  • co模块的前端实现
  • CSS实用技巧
  • Docker下部署自己的LNMP工作环境
  • ESLint简单操作
  • Git的一些常用操作
  • JavaScript工作原理(五):深入了解WebSockets,HTTP/2和SSE,以及如何选择
  • JS函数式编程 数组部分风格 ES6版
  • learning koa2.x
  • Vue官网教程学习过程中值得记录的一些事情
  • 案例分享〡三拾众筹持续交付开发流程支撑创新业务
  • 从0到1:PostCSS 插件开发最佳实践
  • 从setTimeout-setInterval看JS线程
  • 如何使用 OAuth 2.0 将 LinkedIn 集成入 iOS 应用
  • 十年未变!安全,谁之责?(下)
  • 通过来模仿稀土掘金个人页面的布局来学习使用CoordinatorLayout
  • 学习JavaScript数据结构与算法 — 树
  • ​低代码平台的核心价值与优势
  • #14vue3生成表单并跳转到外部地址的方式
  • %3cli%3e连接html页面,html+canvas实现屏幕截取
  • (AtCoder Beginner Contest 340) -- F - S = 1 -- 题解
  • (iPhone/iPad开发)在UIWebView中自定义菜单栏
  • (NSDate) 时间 (time )比较
  • (九)信息融合方式简介
  • (三)elasticsearch 源码之启动流程分析
  • (四)汇编语言——简单程序
  • (一)硬件制作--从零开始自制linux掌上电脑(F1C200S) <嵌入式项目>
  • ****Linux下Mysql的安装和配置
  • .md即markdown文件的基本常用编写语法
  • .NET Core中Emit的使用
  • .NET Framework Client Profile - a Subset of the .NET Framework Redistribution
  • .net 调用海康SDK以及常见的坑解释
  • .Net 垃圾回收机制原理(二)
  • .NET/ASP.NETMVC 大型站点架构设计—迁移Model元数据设置项(自定义元数据提供程序)...
  • .Net6支持的操作系统版本(.net8已来,你还在用.netframework4.5吗)
  • .NET命令行(CLI)常用命令
  • @JSONField或@JsonProperty注解使用
  • [ IOS ] iOS-控制器View的创建和生命周期
  • [BPU部署教程] 教你搞定YOLOV5部署 (版本: 6.2)